cam_isp_context.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. #define CAM_ISP_CTX_DISABLE_RECOVERY_AEB BIT(0)
  96. #define CAM_ISP_CTX_DISABLE_RECOVERY_BUS_OVERFLOW BIT(1)
  97. #define CAM_ISP_CTX_DISABLE_RECOVERY_BUBBLE BIT(2)
  98. /**
  99. * struct cam_isp_ctx_debug - Contains debug parameters
  100. *
  101. * @dentry: Debugfs entry
  102. * @enable_state_monitor_dump: Enable isp state monitor dump
  103. * @enable_cdm_cmd_buff_dump: Enable CDM Command buffer dump
  104. * @disable_internal_recovery: Disable internal kernel recovery mask
  105. *
  106. */
  107. struct cam_isp_ctx_debug {
  108. struct dentry *dentry;
  109. uint32_t enable_state_monitor_dump;
  110. uint8_t enable_cdm_cmd_buff_dump;
  111. uint32_t disable_internal_recovery_mask;
  112. };
  113. /**
  114. * struct cam_isp_ctx_irq_ops - Function table for handling IRQ callbacks
  115. *
  116. * @irq_ops: Array of handle function pointers.
  117. *
  118. */
  119. struct cam_isp_ctx_irq_ops {
  120. cam_isp_hw_event_cb_func irq_ops[CAM_ISP_HW_EVENT_MAX];
  121. };
  122. /**
  123. * struct cam_isp_ctx_req - ISP context request object
  124. *
  125. * @base: Common request object ponter
  126. * @cfg: ISP hardware configuration array
  127. * @num_cfg: Number of ISP hardware configuration entries
  128. * @fence_map_out: Output fence mapping array
  129. * @num_fence_map_out: Number of the output fence map
  130. * @fence_map_in: Input fence mapping array
  131. * @num_fence_map_in: Number of input fence map
  132. * @num_acked: Count to track acked entried for output.
  133. * If count equals the number of fence out, it means
  134. * the request has been completed.
  135. * @num_deferred_acks: Number of buf_dones/acks that are deferred to
  136. * handle or signalled in special scenarios.
  137. * Increment this count instead of num_acked and
  138. * handle the events later where eventually
  139. * increment num_acked.
  140. * @deferred_fence_map_index Saves the indices of fence_map_out for which
  141. * handling of buf_done is deferred.
  142. * @bubble_report: Flag to track if bubble report is active on
  143. * current request
  144. * @hw_update_data: HW update data for this request
  145. * @reapply_type: Determines type of settings to be re-applied
  146. * @event_timestamp: Timestamp for different stage of request
  147. * @cdm_reset_before_apply: For bubble re-apply when buf done not coming set
  148. * to True
  149. *
  150. */
  151. struct cam_isp_ctx_req {
  152. struct cam_ctx_request *base;
  153. struct cam_hw_update_entry *cfg;
  154. uint32_t num_cfg;
  155. struct cam_hw_fence_map_entry *fence_map_out;
  156. uint32_t num_fence_map_out;
  157. struct cam_hw_fence_map_entry *fence_map_in;
  158. uint32_t num_fence_map_in;
  159. uint32_t num_acked;
  160. uint32_t num_deferred_acks;
  161. uint32_t deferred_fence_map_index[CAM_ISP_CTX_RES_MAX];
  162. int32_t bubble_report;
  163. struct cam_isp_prepare_hw_update_data hw_update_data;
  164. enum cam_hw_config_reapply_type reapply_type;
  165. ktime_t event_timestamp
  166. [CAM_ISP_CTX_EVENT_MAX];
  167. bool bubble_detected;
  168. bool cdm_reset_before_apply;
  169. };
  170. /**
  171. * struct cam_isp_context_state_monitor - ISP context state
  172. * monitoring for
  173. * debug purposes
  174. *
  175. * @curr_state: Current sub state that received req
  176. * @trigger: Event type of incoming req
  177. * @req_id: Request id
  178. * @frame_id: Frame id based on SOFs
  179. * @evt_time_stamp Current time stamp
  180. *
  181. */
  182. struct cam_isp_context_state_monitor {
  183. enum cam_isp_ctx_activated_substate curr_state;
  184. enum cam_isp_state_change_trigger trigger;
  185. uint64_t req_id;
  186. int64_t frame_id;
  187. unsigned int evt_time_stamp;
  188. };
  189. /**
  190. * struct cam_isp_context_req_id_info - ISP context request id
  191. * information for bufdone.
  192. *
  193. *@last_bufdone_req_id: Last bufdone request id
  194. *
  195. */
  196. struct cam_isp_context_req_id_info {
  197. int64_t last_bufdone_req_id;
  198. };
  199. /**
  200. *
  201. *
  202. * struct cam_isp_context_event_record - Information for last 20 Events
  203. * for a request; Submit, Apply, EPOCH, RUP, Buf done.
  204. *
  205. * @req_id: Last applied request id
  206. * @timestamp: Timestamp for the event
  207. *
  208. */
  209. struct cam_isp_context_event_record {
  210. uint64_t req_id;
  211. ktime_t timestamp;
  212. };
  213. /**
  214. * struct cam_isp_context - ISP context object
  215. *
  216. * @base: Common context object pointer
  217. * @frame_id: Frame id tracking for the isp context
  218. * @frame_id_meta: Frame id read every epoch for the ctx
  219. * meta from the sensor
  220. * @substate_actiavted: Current substate for the activated state.
  221. * @process_bubble: Atomic variable to check if ctx is still
  222. * processing bubble.
  223. * @substate_machine: ISP substate machine for external interface
  224. * @substate_machine_irq: ISP substate machine for irq handling
  225. * @req_base: Common request object storage
  226. * @req_isp: ISP private request object storage
  227. * @hw_ctx: HW object returned by the acquire device command
  228. * @sof_timestamp_val: Captured time stamp value at sof hw event
  229. * @boot_timestamp: Boot time stamp for a given req_id
  230. * @active_req_cnt: Counter for the active request
  231. * @reported_req_id: Last reported request id
  232. * @subscribe_event: The irq event mask that CRM subscribes to, IFE
  233. * will invoke CRM cb at those event.
  234. * @last_applied_req_id: Last applied request id
  235. * @recovery_req_id: Req ID flagged for internal recovery
  236. * @last_sof_timestamp: SOF timestamp of the last frame
  237. * @bubble_frame_cnt: Count of the frame after bubble
  238. * @aeb_error_cnt: Count number of times a specific AEB error scenario is
  239. * enountered
  240. * @state_monitor_head: Write index to the state monitoring array
  241. * @req_info Request id information about last buf done
  242. * @cam_isp_ctx_state_monitor: State monitoring array
  243. * @event_record_head: Write index to the state monitoring array
  244. * @event_record: Event record array
  245. * @rdi_only_context: Get context type information.
  246. * true, if context is rdi only context
  247. * @offline_context: Indicate whether context is for offline IFE
  248. * @vfps_aux_context: Indicate whether context is for VFPS aux link
  249. * @resume_hw_in_flushed: Indicate whether resume hw in flushed state in vfps case
  250. * @hw_acquired: Indicate whether HW resources are acquired
  251. * @init_received: Indicate whether init config packet is received
  252. * @split_acquire: Indicate whether a separate acquire is expected
  253. * @custom_enabled: Custom HW enabled for this ctx
  254. * @use_frame_header_ts: Use frame header for qtimer ts
  255. * @support_consumed_addr: Indicate whether HW has last consumed addr reg
  256. * @sof_dbg_irq_en: Indicates whether ISP context has enabled debug irqs
  257. * @apply_in_progress Whether request apply is in progress
  258. * @use_default_apply: Use default settings in case of frame skip
  259. * @init_timestamp: Timestamp at which this context is initialized
  260. * @isp_device_type: ISP device type
  261. * @rxd_epoch: Indicate whether epoch has been received. Used to
  262. * decide whether to apply request in offline ctx
  263. * @workq: Worker thread for offline ife
  264. * @trigger_id: ID provided by CRM for each ctx on the link
  265. * @last_bufdone_err_apply_req_id: last bufdone error apply request id
  266. * @v4l2_event_sub_ids contains individual bits representing subscribed v4l2 ids
  267. * @evt_inject_params: event injection parameters
  268. * @aeb_enabled: Indicate if stream is for AEB
  269. * @last_sof_jiffies: Record the jiffies of last sof
  270. * @last_applied_jiffies: Record the jiffiest of last applied req
  271. *
  272. */
  273. struct cam_isp_context {
  274. struct cam_context *base;
  275. uint64_t frame_id;
  276. uint32_t frame_id_meta;
  277. uint32_t substate_activated;
  278. atomic_t process_bubble;
  279. struct cam_ctx_ops *substate_machine;
  280. struct cam_isp_ctx_irq_ops *substate_machine_irq;
  281. struct cam_ctx_request req_base[CAM_ISP_CTX_REQ_MAX];
  282. struct cam_isp_ctx_req req_isp[CAM_ISP_CTX_REQ_MAX];
  283. void *hw_ctx;
  284. uint64_t sof_timestamp_val;
  285. uint64_t boot_timestamp;
  286. int32_t active_req_cnt;
  287. int64_t reported_req_id;
  288. uint64_t reported_frame_id;
  289. uint32_t subscribe_event;
  290. int64_t last_applied_req_id;
  291. uint64_t recovery_req_id;
  292. uint64_t last_sof_timestamp;
  293. uint32_t bubble_frame_cnt;
  294. uint32_t aeb_error_cnt;
  295. atomic64_t state_monitor_head;
  296. struct cam_isp_context_state_monitor cam_isp_ctx_state_monitor[
  297. CAM_ISP_CTX_STATE_MONITOR_MAX_ENTRIES];
  298. struct cam_isp_context_req_id_info req_info;
  299. atomic64_t event_record_head[
  300. CAM_ISP_CTX_EVENT_MAX];
  301. struct cam_isp_context_event_record event_record[
  302. CAM_ISP_CTX_EVENT_MAX][CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES];
  303. bool rdi_only_context;
  304. bool offline_context;
  305. bool vfps_aux_context;
  306. bool resume_hw_in_flushed;
  307. bool hw_acquired;
  308. bool init_received;
  309. bool split_acquire;
  310. bool custom_enabled;
  311. bool use_frame_header_ts;
  312. bool support_consumed_addr;
  313. bool sof_dbg_irq_en;
  314. atomic_t apply_in_progress;
  315. atomic_t internal_recovery_set;
  316. bool use_default_apply;
  317. unsigned int init_timestamp;
  318. uint32_t isp_device_type;
  319. atomic_t rxd_epoch;
  320. struct cam_req_mgr_core_workq *workq;
  321. int32_t trigger_id;
  322. int64_t last_bufdone_err_apply_req_id;
  323. uint32_t v4l2_event_sub_ids;
  324. struct cam_hw_inject_evt_param evt_inject_params;
  325. bool aeb_enabled;
  326. uint64_t last_sof_jiffies;
  327. uint64_t last_applied_jiffies;
  328. };
  329. /**
  330. * struct cam_isp_context_dump_header - ISP context dump header
  331. * @tag: Tag name for the header
  332. * @word_size: Size of word
  333. * @size: Size of data
  334. *
  335. */
  336. struct cam_isp_context_dump_header {
  337. uint8_t tag[CAM_ISP_CONTEXT_DUMP_TAG_MAX_LEN];
  338. uint64_t size;
  339. uint32_t word_size;
  340. };
  341. /** * struct cam_isp_ctx_req_mini_dump - ISP mini dumprequest
  342. *
  343. * @map_out: Output fence mapping
  344. * @map_in: Input fence mapping
  345. * @io_cfg: IO buffer configuration
  346. * @reapply_type: Determines type of settings to be re-applied
  347. * @request_id: Request ID
  348. * @num_fence_map_out: Number of the output fence map
  349. * @num_fence_map_in: Number of input fence map
  350. * @num_io_cfg: Number of ISP hardware configuration entries
  351. * @num_acked: Count to track acked entried for output.
  352. * @num_deferred_acks: Number of buf_dones/acks that are deferred to
  353. * handle or signalled in special scenarios.
  354. * Increment this count instead of num_acked and
  355. * handle the events later where eventually
  356. * increment num_acked.
  357. * @bubble_report: Flag to track if bubble report is active on
  358. * current request
  359. * @bubble_detected: Flag to track if bubble is detected
  360. * @cdm_reset_before_apply: For bubble re-apply when buf done not coming set
  361. * to True
  362. *
  363. */
  364. struct cam_isp_ctx_req_mini_dump {
  365. struct cam_hw_fence_map_entry *map_out;
  366. struct cam_hw_fence_map_entry *map_in;
  367. struct cam_buf_io_cfg *io_cfg;
  368. enum cam_hw_config_reapply_type reapply_type;
  369. uint64_t request_id;
  370. uint8_t num_fence_map_in;
  371. uint8_t num_fence_map_out;
  372. uint8_t num_io_cfg;
  373. uint8_t num_acked;
  374. uint8_t num_deferred_acks;
  375. bool bubble_report;
  376. bool bubble_detected;
  377. bool cdm_reset_before_apply;
  378. };
  379. /**
  380. * struct cam_isp_ctx_mini_dump_info - Isp context mini dump data
  381. *
  382. * @active_list: Active Req list
  383. * @pending_list: Pending req list
  384. * @wait_list: Wait Req List
  385. * @event_record: Event record
  386. * @sof_timestamp_val: Captured time stamp value at sof hw event
  387. * @boot_timestamp: Boot time stamp for a given req_id
  388. * @last_sof_timestamp: SOF timestamp of the last frame
  389. * @init_timestamp: Timestamp at which this context is initialized
  390. * @frame_id: Frame id read every epoch for the ctx
  391. * @reported_req_id: Last reported request id
  392. * @last_applied_req_id: Last applied request id
  393. * @frame_id_meta: Frame id for meta
  394. * @ctx_id: Context id
  395. * @subscribe_event: The irq event mask that CRM subscribes to, IFE
  396. * will invoke CRM cb at those event.
  397. * @bubble_frame_cnt: Count of the frame after bubble
  398. * @isp_device_type: ISP device type
  399. * @active_req_cnt: Counter for the active request
  400. * @trigger_id: ID provided by CRM for each ctx on the link
  401. * @substate_actiavted: Current substate for the activated state.
  402. * @rxd_epoch: Indicate whether epoch has been received. Used to
  403. * decide whether to apply request in offline ctx
  404. * @process_bubble: Atomic variable to check if ctx is still
  405. * processing bubble.
  406. * @apply_in_progress Whether request apply is in progress
  407. * @rdi_only_context: Get context type information.
  408. * true, if context is rdi only context
  409. * @offline_context: Indicate whether context is for offline IFE
  410. * @hw_acquired: Indicate whether HW resources are acquired
  411. * @init_received: Indicate whether init config packet is received
  412. * meta from the sensor
  413. * @split_acquire: Indicate whether a separate acquire is expected
  414. * @custom_enabled: Custom HW enabled for this ctx
  415. * @use_frame_header_ts: Use frame header for qtimer ts
  416. * @support_consumed_addr: Indicate whether HW has last consumed addr reg
  417. * @use_default_apply: Use default settings in case of frame skip
  418. *
  419. */
  420. struct cam_isp_ctx_mini_dump_info {
  421. struct cam_isp_ctx_req_mini_dump *active_list;
  422. struct cam_isp_ctx_req_mini_dump *pending_list;
  423. struct cam_isp_ctx_req_mini_dump *wait_list;
  424. struct cam_isp_context_event_record event_record[
  425. CAM_ISP_CTX_EVENT_MAX][CAM_ISP_CTX_EVENT_RECORD_MAX_ENTRIES];
  426. uint64_t sof_timestamp_val;
  427. uint64_t boot_timestamp;
  428. uint64_t last_sof_timestamp;
  429. uint64_t init_timestamp;
  430. int64_t frame_id;
  431. int64_t reported_req_id;
  432. int64_t last_applied_req_id;
  433. int64_t last_bufdone_err_apply_req_id;
  434. uint32_t frame_id_meta;
  435. uint8_t ctx_id;
  436. uint8_t subscribe_event;
  437. uint8_t bubble_frame_cnt;
  438. uint8_t isp_device_type;
  439. uint8_t active_req_cnt;
  440. uint8_t trigger_id;
  441. uint8_t substate_activated;
  442. uint8_t rxd_epoch;
  443. uint8_t process_bubble;
  444. uint8_t active_cnt;
  445. uint8_t pending_cnt;
  446. uint8_t wait_cnt;
  447. bool apply_in_progress;
  448. bool rdi_only_context;
  449. bool offline_context;
  450. bool hw_acquired;
  451. bool init_received;
  452. bool split_acquire;
  453. bool custom_enabled;
  454. bool use_frame_header_ts;
  455. bool support_consumed_addr;
  456. bool use_default_apply;
  457. };
  458. /**
  459. * cam_isp_context_init()
  460. *
  461. * @brief: Initialization function for the ISP context
  462. *
  463. * @ctx: ISP context obj to be initialized
  464. * @bridge_ops: Bridge call back funciton
  465. * @hw_intf: ISP hw manager interface
  466. * @ctx_id: ID for this context
  467. * @isp_device_type Isp device type
  468. * @img_iommu_hdl IOMMU HDL for image buffers
  469. *
  470. */
  471. int cam_isp_context_init(struct cam_isp_context *ctx,
  472. struct cam_context *ctx_base,
  473. struct cam_req_mgr_kmd_ops *bridge_ops,
  474. struct cam_hw_mgr_intf *hw_intf,
  475. uint32_t ctx_id,
  476. uint32_t isp_device_type,
  477. int img_iommu_hdl);
  478. /**
  479. * cam_isp_context_deinit()
  480. *
  481. * @brief: Deinitialize function for the ISP context
  482. *
  483. * @ctx: ISP context obj to be deinitialized
  484. *
  485. */
  486. int cam_isp_context_deinit(struct cam_isp_context *ctx);
  487. #endif /* __CAM_ISP_CONTEXT_H__ */