cam_isp_context.h 21 KB

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