cam_isp_context.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2017-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #ifndef _CAM_ISP_CONTEXT_H_
  7. #define _CAM_ISP_CONTEXT_H_
  8. #include <linux/spinlock_types.h>
  9. #include <media/cam_isp.h>
  10. #include <media/cam_defs.h>
  11. #include <media/cam_tfe.h>
  12. #include "cam_context.h"
  13. #include "cam_isp_hw_mgr_intf.h"
  14. #include "cam_req_mgr_workq.h"
  15. #define CAM_IFE_QTIMER_MUL_FACTOR 10000
  16. #define CAM_IFE_QTIMER_DIV_FACTOR 192
  17. /*
  18. * Maximum hw resource - This number is based on the maximum
  19. * output port resource. The current maximum resource number
  20. * is 24.
  21. */
  22. #define CAM_ISP_CTX_RES_MAX 24
  23. /* max requests per ctx for isp */
  24. #define CAM_ISP_CTX_REQ_MAX 8
  25. /*
  26. * Maximum entries in state monitoring array for error logging
  27. */
  28. #define CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES 40
  29. /*
  30. * Threshold response time in us beyond which a request is not expected
  31. * to be with IFE hw
  32. */
  33. #define CAM_ISP_CTX_RESPONSE_TIME_THRESHOLD 100000
  34. /* Number of words for dumping isp context */
  35. #define CAM_ISP_CTX_DUMP_NUM_WORDS 5
  36. /* Number of words for dumping isp context events*/
  37. #define CAM_ISP_CTX_DUMP_EVENT_NUM_WORDS 3
  38. /* Number of words for dumping request info*/
  39. #define CAM_ISP_CTX_DUMP_REQUEST_NUM_WORDS 2
  40. /* Maximum entries in event record */
  41. #define CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES 8
  42. /* Maximum length of tag while dumping */
  43. #define CAM_ISP_CONTEXT_DUMP_TAG_MAX_LEN 64
  44. /* AEB error count threshold */
  45. #define CAM_ISP_CONTEXT_AEB_ERROR_CNT_MAX 3
  46. /* forward declaration */
  47. struct cam_isp_context;
  48. /* cam isp context irq handling function type */
  49. typedef int (*cam_isp_hw_event_cb_func)(struct cam_isp_context *ctx_isp,
  50. void *evt_data);
  51. /**
  52. * enum cam_isp_ctx_activated_substate - sub states for activated
  53. *
  54. */
  55. enum cam_isp_ctx_activated_substate {
  56. CAM_ISP_CTX_ACTIVATED_SOF,
  57. CAM_ISP_CTX_ACTIVATED_APPLIED,
  58. CAM_ISP_CTX_ACTIVATED_EPOCH,
  59. CAM_ISP_CTX_ACTIVATED_BUBBLE,
  60. CAM_ISP_CTX_ACTIVATED_BUBBLE_APPLIED,
  61. CAM_ISP_CTX_ACTIVATED_HW_ERROR,
  62. CAM_ISP_CTX_ACTIVATED_HALT,
  63. CAM_ISP_CTX_ACTIVATED_MAX,
  64. };
  65. /**
  66. * enum cam_isp_ctx_event_type - events for a request
  67. *
  68. */
  69. enum cam_isp_ctx_event {
  70. CAM_ISP_CTX_EVENT_SUBMIT,
  71. CAM_ISP_CTX_EVENT_APPLY,
  72. CAM_ISP_CTX_EVENT_EPOCH,
  73. CAM_ISP_CTX_EVENT_RUP,
  74. CAM_ISP_CTX_EVENT_BUFDONE,
  75. CAM_ISP_CTX_EVENT_MAX
  76. };
  77. /**
  78. * enum cam_isp_state_change_trigger - Different types of ISP events
  79. *
  80. */
  81. enum cam_isp_state_change_trigger {
  82. CAM_ISP_STATE_CHANGE_TRIGGER_ERROR,
  83. CAM_ISP_STATE_CHANGE_TRIGGER_APPLIED,
  84. CAM_ISP_STATE_CHANGE_TRIGGER_REG_UPDATE,
  85. CAM_ISP_STATE_CHANGE_TRIGGER_SOF,
  86. CAM_ISP_STATE_CHANGE_TRIGGER_EPOCH,
  87. CAM_ISP_STATE_CHANGE_TRIGGER_DONE,
  88. CAM_ISP_STATE_CHANGE_TRIGGER_EOF,
  89. CAM_ISP_STATE_CHANGE_TRIGGER_FLUSH,
  90. CAM_ISP_STATE_CHANGE_TRIGGER_SEC_EVT_SOF,
  91. CAM_ISP_STATE_CHANGE_TRIGGER_SEC_EVT_EPOCH,
  92. CAM_ISP_STATE_CHANGE_TRIGGER_FRAME_DROP,
  93. CAM_ISP_STATE_CHANGE_TRIGGER_MAX
  94. };
  95. /**
  96. * struct cam_isp_ctx_debug - Contains debug parameters
  97. *
  98. * @dentry: Debugfs entry
  99. * @enable_state_monitor_dump: Enable isp state monitor dump
  100. * @enable_cdm_cmd_buff_dump: Enable CDM Command buffer dump
  101. * @disable_internal_recovery: Disable internal kernel recovery
  102. *
  103. */
  104. struct cam_isp_ctx_debug {
  105. struct dentry *dentry;
  106. uint32_t enable_state_monitor_dump;
  107. uint8_t enable_cdm_cmd_buff_dump;
  108. bool disable_internal_recovery;
  109. };
  110. /**
  111. * struct cam_isp_ctx_irq_ops - Function table for handling IRQ callbacks
  112. *
  113. * @irq_ops: Array of handle function pointers.
  114. *
  115. */
  116. struct cam_isp_ctx_irq_ops {
  117. cam_isp_hw_event_cb_func irq_ops[CAM_ISP_HW_EVENT_MAX];
  118. };
  119. /**
  120. * struct cam_isp_ctx_req - ISP context request object
  121. *
  122. * @base: Common request object ponter
  123. * @cfg: ISP hardware configuration array
  124. * @num_cfg: Number of ISP hardware configuration entries
  125. * @fence_map_out: Output fence mapping array
  126. * @num_fence_map_out: Number of the output fence map
  127. * @fence_map_in: Input fence mapping array
  128. * @num_fence_map_in: Number of input fence map
  129. * @num_acked: Count to track acked entried for output.
  130. * If count equals the number of fence out, it means
  131. * the request has been completed.
  132. * @num_deferred_acks: Number of buf_dones/acks that are deferred to
  133. * handle or signalled in special scenarios.
  134. * Increment this count instead of num_acked and
  135. * handle the events later where eventually
  136. * increment num_acked.
  137. * @deferred_fence_map_index Saves the indices of fence_map_out for which
  138. * handling of buf_done is deferred.
  139. * @bubble_report: Flag to track if bubble report is active on
  140. * current request
  141. * @hw_update_data: HW update data for this request
  142. * @reapply_type: Determines type of settings to be re-applied
  143. * @event_timestamp: Timestamp for different stage of request
  144. * @cdm_reset_before_apply: For bubble re-apply when buf done not coming set
  145. * to True
  146. *
  147. */
  148. struct cam_isp_ctx_req {
  149. struct cam_ctx_request *base;
  150. struct cam_hw_update_entry *cfg;
  151. uint32_t num_cfg;
  152. struct cam_hw_fence_map_entry *fence_map_out;
  153. uint32_t num_fence_map_out;
  154. struct cam_hw_fence_map_entry *fence_map_in;
  155. uint32_t num_fence_map_in;
  156. uint32_t num_acked;
  157. uint32_t num_deferred_acks;
  158. uint32_t deferred_fence_map_index[CAM_ISP_CTX_RES_MAX];
  159. int32_t bubble_report;
  160. struct cam_isp_prepare_hw_update_data hw_update_data;
  161. enum cam_hw_config_reapply_type reapply_type;
  162. ktime_t event_timestamp
  163. [CAM_ISP_CTX_EVENT_MAX];
  164. bool bubble_detected;
  165. bool cdm_reset_before_apply;
  166. };
  167. /**
  168. * struct cam_isp_context_state_monitor - ISP context state
  169. * monitoring for
  170. * debug purposes
  171. *
  172. * @curr_state: Current sub state that received req
  173. * @trigger: Event type of incoming req
  174. * @req_id: Request id
  175. * @frame_id: Frame id based on SOFs
  176. * @evt_time_stamp Current time stamp
  177. *
  178. */
  179. struct cam_isp_context_state_monitor {
  180. enum cam_isp_ctx_activated_substate curr_state;
  181. enum cam_isp_state_change_trigger trigger;
  182. uint64_t req_id;
  183. int64_t frame_id;
  184. unsigned int evt_time_stamp;
  185. };
  186. /**
  187. * struct cam_isp_context_req_id_info - ISP context request id
  188. * information for bufdone.
  189. *
  190. *@last_bufdone_req_id: Last bufdone request id
  191. *
  192. */
  193. struct cam_isp_context_req_id_info {
  194. int64_t last_bufdone_req_id;
  195. };
  196. /**
  197. *
  198. *
  199. * struct cam_isp_context_event_record - Information for last 20 Events
  200. * for a request; Submit, Apply, EPOCH, RUP, Buf done.
  201. *
  202. * @req_id: Last applied request id
  203. * @timestamp: Timestamp for the event
  204. *
  205. */
  206. struct cam_isp_context_event_record {
  207. uint64_t req_id;
  208. ktime_t timestamp;
  209. };
  210. /**
  211. * struct cam_isp_context - ISP context object
  212. *
  213. * @base: Common context object pointer
  214. * @frame_id: Frame id tracking for the isp context
  215. * @frame_id_meta: Frame id read every epoch for the ctx
  216. * meta from the sensor
  217. * @substate_actiavted: Current substate for the activated state.
  218. * @process_bubble: Atomic variable to check if ctx is still
  219. * processing bubble.
  220. * @substate_machine: ISP substate machine for external interface
  221. * @substate_machine_irq: ISP substate machine for irq handling
  222. * @req_base: Common request object storage
  223. * @req_isp: ISP private request object storage
  224. * @hw_ctx: HW object returned by the acquire device command
  225. * @sof_timestamp_val: Captured time stamp value at sof hw event
  226. * @boot_timestamp: Boot time stamp for a given req_id
  227. * @active_req_cnt: Counter for the active request
  228. * @reported_req_id: Last reported request id
  229. * @subscribe_event: The irq event mask that CRM subscribes to, IFE
  230. * will invoke CRM cb at those event.
  231. * @last_applied_req_id: Last applied request id
  232. * @recovery_req_id: Req ID flagged for internal recovery
  233. * @last_sof_timestamp: SOF timestamp of the last frame
  234. * @bubble_frame_cnt: Count of the frame after bubble
  235. * @aeb_error_cnt: Count number of times a specific AEB error scenario is
  236. * enountered
  237. * @state_monitor_head: Write index to the state monitoring array
  238. * @req_info Request id information about last buf done
  239. * @cam_isp_ctx_state_monitor: State monitoring array
  240. * @event_record_head: Write index to the state monitoring array
  241. * @event_record: Event record array
  242. * @rdi_only_context: Get context type information.
  243. * true, if context is rdi only context
  244. * @offline_context: Indicate whether context is for offline IFE
  245. * @vfps_aux_context: Indicate whether context is for VFPS aux link
  246. * @resume_hw_in_flushed: Indicate whether resume hw in flushed state in vfps case
  247. * @hw_acquired: Indicate whether HW resources are acquired
  248. * @init_received: Indicate whether init config packet is received
  249. * @split_acquire: Indicate whether a separate acquire is expected
  250. * @custom_enabled: Custom HW enabled for this ctx
  251. * @use_frame_header_ts: Use frame header for qtimer ts
  252. * @support_consumed_addr: Indicate whether HW has last consumed addr reg
  253. * @apply_in_progress Whether request apply is in progress
  254. * @use_default_apply: Use default settings in case of frame skip
  255. * @init_timestamp: Timestamp at which this context is initialized
  256. * @isp_device_type: ISP device type
  257. * @rxd_epoch: Indicate whether epoch has been received. Used to
  258. * decide whether to apply request in offline ctx
  259. * @workq: Worker thread for offline ife
  260. * @trigger_id: ID provided by CRM for each ctx on the link
  261. * @last_bufdone_err_apply_req_id: last bufdone error apply request id
  262. * @v4l2_event_sub_ids contains individual bits representing subscribed v4l2 ids
  263. * @evt_inject_params: event injection parameters
  264. * @aeb_enabled: Indicate if stream is for AEB
  265. * @do_internal_recovery: Enable KMD halt/reset/resume internal recovery
  266. *
  267. */
  268. struct cam_isp_context {
  269. struct cam_context *base;
  270. uint64_t frame_id;
  271. uint32_t frame_id_meta;
  272. uint32_t substate_activated;
  273. atomic_t process_bubble;
  274. struct cam_ctx_ops *substate_machine;
  275. struct cam_isp_ctx_irq_ops *substate_machine_irq;
  276. struct cam_ctx_request req_base[CAM_ISP_CTX_REQ_MAX];
  277. struct cam_isp_ctx_req req_isp[CAM_ISP_CTX_REQ_MAX];
  278. void *hw_ctx;
  279. uint64_t sof_timestamp_val;
  280. uint64_t boot_timestamp;
  281. int32_t active_req_cnt;
  282. int64_t reported_req_id;
  283. uint64_t reported_frame_id;
  284. uint32_t subscribe_event;
  285. int64_t last_applied_req_id;
  286. uint64_t recovery_req_id;
  287. uint64_t last_sof_timestamp;
  288. uint32_t bubble_frame_cnt;
  289. uint32_t aeb_error_cnt;
  290. atomic64_t state_monitor_head;
  291. struct cam_isp_context_state_monitor cam_isp_ctx_state_monitor[
  292. CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES];
  293. struct cam_isp_context_req_id_info req_info;
  294. atomic64_t event_record_head[
  295. CAM_ISP_CTX_EVENT_MAX];
  296. struct cam_isp_context_event_record event_record[
  297. CAM_ISP_CTX_EVENT_MAX][CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES];
  298. bool rdi_only_context;
  299. bool offline_context;
  300. bool vfps_aux_context;
  301. bool resume_hw_in_flushed;
  302. bool hw_acquired;
  303. bool init_received;
  304. bool split_acquire;
  305. bool custom_enabled;
  306. bool use_frame_header_ts;
  307. bool support_consumed_addr;
  308. atomic_t apply_in_progress;
  309. atomic_t internal_recovery_set;
  310. bool use_default_apply;
  311. unsigned int init_timestamp;
  312. uint32_t isp_device_type;
  313. atomic_t rxd_epoch;
  314. struct cam_req_mgr_core_workq *workq;
  315. int32_t trigger_id;
  316. int64_t last_bufdone_err_apply_req_id;
  317. uint32_t v4l2_event_sub_ids;
  318. struct cam_hw_inject_evt_param evt_inject_params;
  319. bool aeb_enabled;
  320. bool do_internal_recovery;
  321. };
  322. /**
  323. * struct cam_isp_context_dump_header - ISP context dump header
  324. * @tag: Tag name for the header
  325. * @word_size: Size of word
  326. * @size: Size of data
  327. *
  328. */
  329. struct cam_isp_context_dump_header {
  330. uint8_t tag[CAM_ISP_CONTEXT_DUMP_TAG_MAX_LEN];
  331. uint64_t size;
  332. uint32_t word_size;
  333. };
  334. /** * struct cam_isp_ctx_req_mini_dump - ISP mini dumprequest
  335. *
  336. * @map_out: Output fence mapping
  337. * @map_in: Input fence mapping
  338. * @io_cfg: IO buffer configuration
  339. * @reapply_type: Determines type of settings to be re-applied
  340. * @request_id: Request ID
  341. * @num_fence_map_out: Number of the output fence map
  342. * @num_fence_map_in: Number of input fence map
  343. * @num_io_cfg: Number of ISP hardware configuration entries
  344. * @num_acked: Count to track acked entried for output.
  345. * @num_deferred_acks: Number of buf_dones/acks that are deferred to
  346. * handle or signalled in special scenarios.
  347. * Increment this count instead of num_acked and
  348. * handle the events later where eventually
  349. * increment num_acked.
  350. * @bubble_report: Flag to track if bubble report is active on
  351. * current request
  352. * @bubble_detected: Flag to track if bubble is detected
  353. * @cdm_reset_before_apply: For bubble re-apply when buf done not coming set
  354. * to True
  355. *
  356. */
  357. struct cam_isp_ctx_req_mini_dump {
  358. struct cam_hw_fence_map_entry *map_out;
  359. struct cam_hw_fence_map_entry *map_in;
  360. struct cam_buf_io_cfg *io_cfg;
  361. enum cam_hw_config_reapply_type reapply_type;
  362. uint64_t request_id;
  363. uint8_t num_fence_map_in;
  364. uint8_t num_fence_map_out;
  365. uint8_t num_io_cfg;
  366. uint8_t num_acked;
  367. uint8_t num_deferred_acks;
  368. bool bubble_report;
  369. bool bubble_detected;
  370. bool cdm_reset_before_apply;
  371. };
  372. /**
  373. * struct cam_isp_ctx_mini_dump_info - Isp context mini dump data
  374. *
  375. * @active_list: Active Req list
  376. * @pending_list: Pending req list
  377. * @wait_list: Wait Req List
  378. * @event_record: Event record
  379. * @sof_timestamp_val: Captured time stamp value at sof hw event
  380. * @boot_timestamp: Boot time stamp for a given req_id
  381. * @last_sof_timestamp: SOF timestamp of the last frame
  382. * @init_timestamp: Timestamp at which this context is initialized
  383. * @frame_id: Frame id read every epoch for the ctx
  384. * @reported_req_id: Last reported request id
  385. * @last_applied_req_id: Last applied request id
  386. * @frame_id_meta: Frame id for meta
  387. * @ctx_id: Context id
  388. * @subscribe_event: The irq event mask that CRM subscribes to, IFE
  389. * will invoke CRM cb at those event.
  390. * @bubble_frame_cnt: Count of the frame after bubble
  391. * @isp_device_type: ISP device type
  392. * @active_req_cnt: Counter for the active request
  393. * @trigger_id: ID provided by CRM for each ctx on the link
  394. * @substate_actiavted: Current substate for the activated state.
  395. * @rxd_epoch: Indicate whether epoch has been received. Used to
  396. * decide whether to apply request in offline ctx
  397. * @process_bubble: Atomic variable to check if ctx is still
  398. * processing bubble.
  399. * @apply_in_progress Whether request apply is in progress
  400. * @rdi_only_context: Get context type information.
  401. * true, if context is rdi only context
  402. * @offline_context: Indicate whether context is for offline IFE
  403. * @hw_acquired: Indicate whether HW resources are acquired
  404. * @init_received: Indicate whether init config packet is received
  405. * meta from the sensor
  406. * @split_acquire: Indicate whether a separate acquire is expected
  407. * @custom_enabled: Custom HW enabled for this ctx
  408. * @use_frame_header_ts: Use frame header for qtimer ts
  409. * @support_consumed_addr: Indicate whether HW has last consumed addr reg
  410. * @use_default_apply: Use default settings in case of frame skip
  411. *
  412. */
  413. struct cam_isp_ctx_mini_dump_info {
  414. struct cam_isp_ctx_req_mini_dump *active_list;
  415. struct cam_isp_ctx_req_mini_dump *pending_list;
  416. struct cam_isp_ctx_req_mini_dump *wait_list;
  417. struct cam_isp_context_event_record event_record[
  418. CAM_ISP_CTX_EVENT_MAX][CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES];
  419. uint64_t sof_timestamp_val;
  420. uint64_t boot_timestamp;
  421. uint64_t last_sof_timestamp;
  422. uint64_t init_timestamp;
  423. int64_t frame_id;
  424. int64_t reported_req_id;
  425. int64_t last_applied_req_id;
  426. int64_t last_bufdone_err_apply_req_id;
  427. uint32_t frame_id_meta;
  428. uint8_t ctx_id;
  429. uint8_t subscribe_event;
  430. uint8_t bubble_frame_cnt;
  431. uint8_t isp_device_type;
  432. uint8_t active_req_cnt;
  433. uint8_t trigger_id;
  434. uint8_t substate_activated;
  435. uint8_t rxd_epoch;
  436. uint8_t process_bubble;
  437. uint8_t active_cnt;
  438. uint8_t pending_cnt;
  439. uint8_t wait_cnt;
  440. bool apply_in_progress;
  441. bool rdi_only_context;
  442. bool offline_context;
  443. bool hw_acquired;
  444. bool init_received;
  445. bool split_acquire;
  446. bool custom_enabled;
  447. bool use_frame_header_ts;
  448. bool support_consumed_addr;
  449. bool use_default_apply;
  450. };
  451. /**
  452. * cam_isp_context_init()
  453. *
  454. * @brief: Initialization function for the ISP context
  455. *
  456. * @ctx: ISP context obj to be initialized
  457. * @bridge_ops: Bridge call back funciton
  458. * @hw_intf: ISP hw manager interface
  459. * @ctx_id: ID for this context
  460. * @isp_device_type Isp device type
  461. * @img_iommu_hdl IOMMU HDL for image buffers
  462. *
  463. */
  464. int cam_isp_context_init(struct cam_isp_context *ctx,
  465. struct cam_context *ctx_base,
  466. struct cam_req_mgr_kmd_ops *bridge_ops,
  467. struct cam_hw_mgr_intf *hw_intf,
  468. uint32_t ctx_id,
  469. uint32_t isp_device_type,
  470. int img_iommu_hdl);
  471. /**
  472. * cam_isp_context_deinit()
  473. *
  474. * @brief: Deinitialize function for the ISP context
  475. *
  476. * @ctx: ISP context obj to be deinitialized
  477. *
  478. */
  479. int cam_isp_context_deinit(struct cam_isp_context *ctx);
  480. #endif /* __CAM_ISP_CONTEXT_H__ */