sti_hqvdp.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417
  1. /*
  2. * Copyright (C) STMicroelectronics SA 2014
  3. * Authors: Fabien Dessenne <fabien.dessenne@st.com> for STMicroelectronics.
  4. * License terms: GNU General Public License (GPL), version 2
  5. */
  6. #include <linux/component.h>
  7. #include <linux/firmware.h>
  8. #include <linux/reset.h>
  9. #include <linux/seq_file.h>
  10. #include <drm/drm_atomic.h>
  11. #include <drm/drm_fb_cma_helper.h>
  12. #include <drm/drm_gem_cma_helper.h>
  13. #include "sti_compositor.h"
  14. #include "sti_hqvdp_lut.h"
  15. #include "sti_plane.h"
  16. #include "sti_vtg.h"
  17. #include "sti_drv.h"
  18. /* Firmware name */
  19. #define HQVDP_FMW_NAME "hqvdp-stih407.bin"
  20. /* Regs address */
  21. #define HQVDP_DMEM 0x00000000 /* 0x00000000 */
  22. #define HQVDP_PMEM 0x00040000 /* 0x00040000 */
  23. #define HQVDP_RD_PLUG 0x000E0000 /* 0x000E0000 */
  24. #define HQVDP_RD_PLUG_CONTROL (HQVDP_RD_PLUG + 0x1000) /* 0x000E1000 */
  25. #define HQVDP_RD_PLUG_PAGE_SIZE (HQVDP_RD_PLUG + 0x1004) /* 0x000E1004 */
  26. #define HQVDP_RD_PLUG_MIN_OPC (HQVDP_RD_PLUG + 0x1008) /* 0x000E1008 */
  27. #define HQVDP_RD_PLUG_MAX_OPC (HQVDP_RD_PLUG + 0x100C) /* 0x000E100C */
  28. #define HQVDP_RD_PLUG_MAX_CHK (HQVDP_RD_PLUG + 0x1010) /* 0x000E1010 */
  29. #define HQVDP_RD_PLUG_MAX_MSG (HQVDP_RD_PLUG + 0x1014) /* 0x000E1014 */
  30. #define HQVDP_RD_PLUG_MIN_SPACE (HQVDP_RD_PLUG + 0x1018) /* 0x000E1018 */
  31. #define HQVDP_WR_PLUG 0x000E2000 /* 0x000E2000 */
  32. #define HQVDP_WR_PLUG_CONTROL (HQVDP_WR_PLUG + 0x1000) /* 0x000E3000 */
  33. #define HQVDP_WR_PLUG_PAGE_SIZE (HQVDP_WR_PLUG + 0x1004) /* 0x000E3004 */
  34. #define HQVDP_WR_PLUG_MIN_OPC (HQVDP_WR_PLUG + 0x1008) /* 0x000E3008 */
  35. #define HQVDP_WR_PLUG_MAX_OPC (HQVDP_WR_PLUG + 0x100C) /* 0x000E300C */
  36. #define HQVDP_WR_PLUG_MAX_CHK (HQVDP_WR_PLUG + 0x1010) /* 0x000E3010 */
  37. #define HQVDP_WR_PLUG_MAX_MSG (HQVDP_WR_PLUG + 0x1014) /* 0x000E3014 */
  38. #define HQVDP_WR_PLUG_MIN_SPACE (HQVDP_WR_PLUG + 0x1018) /* 0x000E3018 */
  39. #define HQVDP_MBX 0x000E4000 /* 0x000E4000 */
  40. #define HQVDP_MBX_IRQ_TO_XP70 (HQVDP_MBX + 0x0000) /* 0x000E4000 */
  41. #define HQVDP_MBX_INFO_HOST (HQVDP_MBX + 0x0004) /* 0x000E4004 */
  42. #define HQVDP_MBX_IRQ_TO_HOST (HQVDP_MBX + 0x0008) /* 0x000E4008 */
  43. #define HQVDP_MBX_INFO_XP70 (HQVDP_MBX + 0x000C) /* 0x000E400C */
  44. #define HQVDP_MBX_SW_RESET_CTRL (HQVDP_MBX + 0x0010) /* 0x000E4010 */
  45. #define HQVDP_MBX_STARTUP_CTRL1 (HQVDP_MBX + 0x0014) /* 0x000E4014 */
  46. #define HQVDP_MBX_STARTUP_CTRL2 (HQVDP_MBX + 0x0018) /* 0x000E4018 */
  47. #define HQVDP_MBX_GP_STATUS (HQVDP_MBX + 0x001C) /* 0x000E401C */
  48. #define HQVDP_MBX_NEXT_CMD (HQVDP_MBX + 0x0020) /* 0x000E4020 */
  49. #define HQVDP_MBX_CURRENT_CMD (HQVDP_MBX + 0x0024) /* 0x000E4024 */
  50. #define HQVDP_MBX_SOFT_VSYNC (HQVDP_MBX + 0x0028) /* 0x000E4028 */
  51. /* Plugs config */
  52. #define PLUG_CONTROL_ENABLE 0x00000001
  53. #define PLUG_PAGE_SIZE_256 0x00000002
  54. #define PLUG_MIN_OPC_8 0x00000003
  55. #define PLUG_MAX_OPC_64 0x00000006
  56. #define PLUG_MAX_CHK_2X 0x00000001
  57. #define PLUG_MAX_MSG_1X 0x00000000
  58. #define PLUG_MIN_SPACE_1 0x00000000
  59. /* SW reset CTRL */
  60. #define SW_RESET_CTRL_FULL BIT(0)
  61. #define SW_RESET_CTRL_CORE BIT(1)
  62. /* Startup ctrl 1 */
  63. #define STARTUP_CTRL1_RST_DONE BIT(0)
  64. #define STARTUP_CTRL1_AUTH_IDLE BIT(2)
  65. /* Startup ctrl 2 */
  66. #define STARTUP_CTRL2_FETCH_EN BIT(1)
  67. /* Info xP70 */
  68. #define INFO_XP70_FW_READY BIT(15)
  69. #define INFO_XP70_FW_PROCESSING BIT(14)
  70. #define INFO_XP70_FW_INITQUEUES BIT(13)
  71. /* SOFT_VSYNC */
  72. #define SOFT_VSYNC_HW 0x00000000
  73. #define SOFT_VSYNC_SW_CMD 0x00000001
  74. #define SOFT_VSYNC_SW_CTRL_IRQ 0x00000003
  75. /* Reset & boot poll config */
  76. #define POLL_MAX_ATTEMPT 50
  77. #define POLL_DELAY_MS 20
  78. #define SCALE_FACTOR 8192
  79. #define SCALE_MAX_FOR_LEG_LUT_F 4096
  80. #define SCALE_MAX_FOR_LEG_LUT_E 4915
  81. #define SCALE_MAX_FOR_LEG_LUT_D 6654
  82. #define SCALE_MAX_FOR_LEG_LUT_C 8192
  83. enum sti_hvsrc_orient {
  84. HVSRC_HORI,
  85. HVSRC_VERT
  86. };
  87. /* Command structures */
  88. struct sti_hqvdp_top {
  89. u32 config;
  90. u32 mem_format;
  91. u32 current_luma;
  92. u32 current_enh_luma;
  93. u32 current_right_luma;
  94. u32 current_enh_right_luma;
  95. u32 current_chroma;
  96. u32 current_enh_chroma;
  97. u32 current_right_chroma;
  98. u32 current_enh_right_chroma;
  99. u32 output_luma;
  100. u32 output_chroma;
  101. u32 luma_src_pitch;
  102. u32 luma_enh_src_pitch;
  103. u32 luma_right_src_pitch;
  104. u32 luma_enh_right_src_pitch;
  105. u32 chroma_src_pitch;
  106. u32 chroma_enh_src_pitch;
  107. u32 chroma_right_src_pitch;
  108. u32 chroma_enh_right_src_pitch;
  109. u32 luma_processed_pitch;
  110. u32 chroma_processed_pitch;
  111. u32 input_frame_size;
  112. u32 input_viewport_ori;
  113. u32 input_viewport_ori_right;
  114. u32 input_viewport_size;
  115. u32 left_view_border_width;
  116. u32 right_view_border_width;
  117. u32 left_view_3d_offset_width;
  118. u32 right_view_3d_offset_width;
  119. u32 side_stripe_color;
  120. u32 crc_reset_ctrl;
  121. };
  122. /* Configs for interlaced : no IT, no pass thru, 3 fields */
  123. #define TOP_CONFIG_INTER_BTM 0x00000000
  124. #define TOP_CONFIG_INTER_TOP 0x00000002
  125. /* Config for progressive : no IT, no pass thru, 3 fields */
  126. #define TOP_CONFIG_PROGRESSIVE 0x00000001
  127. /* Default MemFormat: in=420_raster_dual out=444_raster;opaque Mem2Tv mode */
  128. #define TOP_MEM_FORMAT_DFLT 0x00018060
  129. /* Min/Max size */
  130. #define MAX_WIDTH 0x1FFF
  131. #define MAX_HEIGHT 0x0FFF
  132. #define MIN_WIDTH 0x0030
  133. #define MIN_HEIGHT 0x0010
  134. struct sti_hqvdp_vc1re {
  135. u32 ctrl_prv_csdi;
  136. u32 ctrl_cur_csdi;
  137. u32 ctrl_nxt_csdi;
  138. u32 ctrl_cur_fmd;
  139. u32 ctrl_nxt_fmd;
  140. };
  141. struct sti_hqvdp_fmd {
  142. u32 config;
  143. u32 viewport_ori;
  144. u32 viewport_size;
  145. u32 next_next_luma;
  146. u32 next_next_right_luma;
  147. u32 next_next_next_luma;
  148. u32 next_next_next_right_luma;
  149. u32 threshold_scd;
  150. u32 threshold_rfd;
  151. u32 threshold_move;
  152. u32 threshold_cfd;
  153. };
  154. struct sti_hqvdp_csdi {
  155. u32 config;
  156. u32 config2;
  157. u32 dcdi_config;
  158. u32 prev_luma;
  159. u32 prev_enh_luma;
  160. u32 prev_right_luma;
  161. u32 prev_enh_right_luma;
  162. u32 next_luma;
  163. u32 next_enh_luma;
  164. u32 next_right_luma;
  165. u32 next_enh_right_luma;
  166. u32 prev_chroma;
  167. u32 prev_enh_chroma;
  168. u32 prev_right_chroma;
  169. u32 prev_enh_right_chroma;
  170. u32 next_chroma;
  171. u32 next_enh_chroma;
  172. u32 next_right_chroma;
  173. u32 next_enh_right_chroma;
  174. u32 prev_motion;
  175. u32 prev_right_motion;
  176. u32 cur_motion;
  177. u32 cur_right_motion;
  178. u32 next_motion;
  179. u32 next_right_motion;
  180. };
  181. /* Config for progressive: by pass */
  182. #define CSDI_CONFIG_PROG 0x00000000
  183. /* Config for directional deinterlacing without motion */
  184. #define CSDI_CONFIG_INTER_DIR 0x00000016
  185. /* Additional configs for fader, blender, motion,... deinterlace algorithms */
  186. #define CSDI_CONFIG2_DFLT 0x000001B3
  187. #define CSDI_DCDI_CONFIG_DFLT 0x00203803
  188. struct sti_hqvdp_hvsrc {
  189. u32 hor_panoramic_ctrl;
  190. u32 output_picture_size;
  191. u32 init_horizontal;
  192. u32 init_vertical;
  193. u32 param_ctrl;
  194. u32 yh_coef[NB_COEF];
  195. u32 ch_coef[NB_COEF];
  196. u32 yv_coef[NB_COEF];
  197. u32 cv_coef[NB_COEF];
  198. u32 hori_shift;
  199. u32 vert_shift;
  200. };
  201. /* Default ParamCtrl: all controls enabled */
  202. #define HVSRC_PARAM_CTRL_DFLT 0xFFFFFFFF
  203. struct sti_hqvdp_iqi {
  204. u32 config;
  205. u32 demo_wind_size;
  206. u32 pk_config;
  207. u32 coeff0_coeff1;
  208. u32 coeff2_coeff3;
  209. u32 coeff4;
  210. u32 pk_lut;
  211. u32 pk_gain;
  212. u32 pk_coring_level;
  213. u32 cti_config;
  214. u32 le_config;
  215. u32 le_lut[64];
  216. u32 con_bri;
  217. u32 sat_gain;
  218. u32 pxf_conf;
  219. u32 default_color;
  220. };
  221. /* Default Config : IQI bypassed */
  222. #define IQI_CONFIG_DFLT 0x00000001
  223. /* Default Contrast & Brightness gain = 256 */
  224. #define IQI_CON_BRI_DFLT 0x00000100
  225. /* Default Saturation gain = 256 */
  226. #define IQI_SAT_GAIN_DFLT 0x00000100
  227. /* Default PxfConf : P2I bypassed */
  228. #define IQI_PXF_CONF_DFLT 0x00000001
  229. struct sti_hqvdp_top_status {
  230. u32 processing_time;
  231. u32 input_y_crc;
  232. u32 input_uv_crc;
  233. };
  234. struct sti_hqvdp_fmd_status {
  235. u32 fmd_repeat_move_status;
  236. u32 fmd_scene_count_status;
  237. u32 cfd_sum;
  238. u32 field_sum;
  239. u32 next_y_fmd_crc;
  240. u32 next_next_y_fmd_crc;
  241. u32 next_next_next_y_fmd_crc;
  242. };
  243. struct sti_hqvdp_csdi_status {
  244. u32 prev_y_csdi_crc;
  245. u32 cur_y_csdi_crc;
  246. u32 next_y_csdi_crc;
  247. u32 prev_uv_csdi_crc;
  248. u32 cur_uv_csdi_crc;
  249. u32 next_uv_csdi_crc;
  250. u32 y_csdi_crc;
  251. u32 uv_csdi_crc;
  252. u32 uv_cup_crc;
  253. u32 mot_csdi_crc;
  254. u32 mot_cur_csdi_crc;
  255. u32 mot_prev_csdi_crc;
  256. };
  257. struct sti_hqvdp_hvsrc_status {
  258. u32 y_hvsrc_crc;
  259. u32 u_hvsrc_crc;
  260. u32 v_hvsrc_crc;
  261. };
  262. struct sti_hqvdp_iqi_status {
  263. u32 pxf_it_status;
  264. u32 y_iqi_crc;
  265. u32 u_iqi_crc;
  266. u32 v_iqi_crc;
  267. };
  268. /* Main commands. We use 2 commands one being processed by the firmware, one
  269. * ready to be fetched upon next Vsync*/
  270. #define NB_VDP_CMD 2
  271. struct sti_hqvdp_cmd {
  272. struct sti_hqvdp_top top;
  273. struct sti_hqvdp_vc1re vc1re;
  274. struct sti_hqvdp_fmd fmd;
  275. struct sti_hqvdp_csdi csdi;
  276. struct sti_hqvdp_hvsrc hvsrc;
  277. struct sti_hqvdp_iqi iqi;
  278. struct sti_hqvdp_top_status top_status;
  279. struct sti_hqvdp_fmd_status fmd_status;
  280. struct sti_hqvdp_csdi_status csdi_status;
  281. struct sti_hqvdp_hvsrc_status hvsrc_status;
  282. struct sti_hqvdp_iqi_status iqi_status;
  283. };
  284. /*
  285. * STI HQVDP structure
  286. *
  287. * @dev: driver device
  288. * @drm_dev: the drm device
  289. * @regs: registers
  290. * @plane: plane structure for hqvdp it self
  291. * @clk: IP clock
  292. * @clk_pix_main: pix main clock
  293. * @reset: reset control
  294. * @vtg_nb: notifier to handle VTG Vsync
  295. * @btm_field_pending: is there any bottom field (interlaced frame) to display
  296. * @hqvdp_cmd: buffer of commands
  297. * @hqvdp_cmd_paddr: physical address of hqvdp_cmd
  298. * @vtg: vtg for main data path
  299. * @xp70_initialized: true if xp70 is already initialized
  300. * @vtg_registered: true if registered to VTG
  301. */
  302. struct sti_hqvdp {
  303. struct device *dev;
  304. struct drm_device *drm_dev;
  305. void __iomem *regs;
  306. struct sti_plane plane;
  307. struct clk *clk;
  308. struct clk *clk_pix_main;
  309. struct reset_control *reset;
  310. struct notifier_block vtg_nb;
  311. bool btm_field_pending;
  312. void *hqvdp_cmd;
  313. u32 hqvdp_cmd_paddr;
  314. struct sti_vtg *vtg;
  315. bool xp70_initialized;
  316. bool vtg_registered;
  317. };
  318. #define to_sti_hqvdp(x) container_of(x, struct sti_hqvdp, plane)
  319. static const uint32_t hqvdp_supported_formats[] = {
  320. DRM_FORMAT_NV12,
  321. };
  322. /**
  323. * sti_hqvdp_get_free_cmd
  324. * @hqvdp: hqvdp structure
  325. *
  326. * Look for a hqvdp_cmd that is not being used (or about to be used) by the FW.
  327. *
  328. * RETURNS:
  329. * the offset of the command to be used.
  330. * -1 in error cases
  331. */
  332. static int sti_hqvdp_get_free_cmd(struct sti_hqvdp *hqvdp)
  333. {
  334. u32 curr_cmd, next_cmd;
  335. u32 cmd = hqvdp->hqvdp_cmd_paddr;
  336. int i;
  337. curr_cmd = readl(hqvdp->regs + HQVDP_MBX_CURRENT_CMD);
  338. next_cmd = readl(hqvdp->regs + HQVDP_MBX_NEXT_CMD);
  339. for (i = 0; i < NB_VDP_CMD; i++) {
  340. if ((cmd != curr_cmd) && (cmd != next_cmd))
  341. return i * sizeof(struct sti_hqvdp_cmd);
  342. cmd += sizeof(struct sti_hqvdp_cmd);
  343. }
  344. return -1;
  345. }
  346. /**
  347. * sti_hqvdp_get_curr_cmd
  348. * @hqvdp: hqvdp structure
  349. *
  350. * Look for the hqvdp_cmd that is being used by the FW.
  351. *
  352. * RETURNS:
  353. * the offset of the command to be used.
  354. * -1 in error cases
  355. */
  356. static int sti_hqvdp_get_curr_cmd(struct sti_hqvdp *hqvdp)
  357. {
  358. u32 curr_cmd;
  359. u32 cmd = hqvdp->hqvdp_cmd_paddr;
  360. unsigned int i;
  361. curr_cmd = readl(hqvdp->regs + HQVDP_MBX_CURRENT_CMD);
  362. for (i = 0; i < NB_VDP_CMD; i++) {
  363. if (cmd == curr_cmd)
  364. return i * sizeof(struct sti_hqvdp_cmd);
  365. cmd += sizeof(struct sti_hqvdp_cmd);
  366. }
  367. return -1;
  368. }
  369. /**
  370. * sti_hqvdp_get_next_cmd
  371. * @hqvdp: hqvdp structure
  372. *
  373. * Look for the next hqvdp_cmd that will be used by the FW.
  374. *
  375. * RETURNS:
  376. * the offset of the next command that will be used.
  377. * -1 in error cases
  378. */
  379. static int sti_hqvdp_get_next_cmd(struct sti_hqvdp *hqvdp)
  380. {
  381. int next_cmd;
  382. dma_addr_t cmd = hqvdp->hqvdp_cmd_paddr;
  383. unsigned int i;
  384. next_cmd = readl(hqvdp->regs + HQVDP_MBX_NEXT_CMD);
  385. for (i = 0; i < NB_VDP_CMD; i++) {
  386. if (cmd == next_cmd)
  387. return i * sizeof(struct sti_hqvdp_cmd);
  388. cmd += sizeof(struct sti_hqvdp_cmd);
  389. }
  390. return -1;
  391. }
  392. #define DBGFS_DUMP(reg) seq_printf(s, "\n %-25s 0x%08X", #reg, \
  393. readl(hqvdp->regs + reg))
  394. static const char *hqvdp_dbg_get_lut(u32 *coef)
  395. {
  396. if (!memcmp(coef, coef_lut_a_legacy, 16))
  397. return "LUT A";
  398. if (!memcmp(coef, coef_lut_b, 16))
  399. return "LUT B";
  400. if (!memcmp(coef, coef_lut_c_y_legacy, 16))
  401. return "LUT C Y";
  402. if (!memcmp(coef, coef_lut_c_c_legacy, 16))
  403. return "LUT C C";
  404. if (!memcmp(coef, coef_lut_d_y_legacy, 16))
  405. return "LUT D Y";
  406. if (!memcmp(coef, coef_lut_d_c_legacy, 16))
  407. return "LUT D C";
  408. if (!memcmp(coef, coef_lut_e_y_legacy, 16))
  409. return "LUT E Y";
  410. if (!memcmp(coef, coef_lut_e_c_legacy, 16))
  411. return "LUT E C";
  412. if (!memcmp(coef, coef_lut_f_y_legacy, 16))
  413. return "LUT F Y";
  414. if (!memcmp(coef, coef_lut_f_c_legacy, 16))
  415. return "LUT F C";
  416. return "<UNKNOWN>";
  417. }
  418. static void hqvdp_dbg_dump_cmd(struct seq_file *s, struct sti_hqvdp_cmd *c)
  419. {
  420. int src_w, src_h, dst_w, dst_h;
  421. seq_puts(s, "\n\tTOP:");
  422. seq_printf(s, "\n\t %-20s 0x%08X", "Config", c->top.config);
  423. switch (c->top.config) {
  424. case TOP_CONFIG_PROGRESSIVE:
  425. seq_puts(s, "\tProgressive");
  426. break;
  427. case TOP_CONFIG_INTER_TOP:
  428. seq_puts(s, "\tInterlaced, top field");
  429. break;
  430. case TOP_CONFIG_INTER_BTM:
  431. seq_puts(s, "\tInterlaced, bottom field");
  432. break;
  433. default:
  434. seq_puts(s, "\t<UNKNOWN>");
  435. break;
  436. }
  437. seq_printf(s, "\n\t %-20s 0x%08X", "MemFormat", c->top.mem_format);
  438. seq_printf(s, "\n\t %-20s 0x%08X", "CurrentY", c->top.current_luma);
  439. seq_printf(s, "\n\t %-20s 0x%08X", "CurrentC", c->top.current_chroma);
  440. seq_printf(s, "\n\t %-20s 0x%08X", "YSrcPitch", c->top.luma_src_pitch);
  441. seq_printf(s, "\n\t %-20s 0x%08X", "CSrcPitch",
  442. c->top.chroma_src_pitch);
  443. seq_printf(s, "\n\t %-20s 0x%08X", "InputFrameSize",
  444. c->top.input_frame_size);
  445. seq_printf(s, "\t%dx%d",
  446. c->top.input_frame_size & 0x0000FFFF,
  447. c->top.input_frame_size >> 16);
  448. seq_printf(s, "\n\t %-20s 0x%08X", "InputViewportSize",
  449. c->top.input_viewport_size);
  450. src_w = c->top.input_viewport_size & 0x0000FFFF;
  451. src_h = c->top.input_viewport_size >> 16;
  452. seq_printf(s, "\t%dx%d", src_w, src_h);
  453. seq_puts(s, "\n\tHVSRC:");
  454. seq_printf(s, "\n\t %-20s 0x%08X", "OutputPictureSize",
  455. c->hvsrc.output_picture_size);
  456. dst_w = c->hvsrc.output_picture_size & 0x0000FFFF;
  457. dst_h = c->hvsrc.output_picture_size >> 16;
  458. seq_printf(s, "\t%dx%d", dst_w, dst_h);
  459. seq_printf(s, "\n\t %-20s 0x%08X", "ParamCtrl", c->hvsrc.param_ctrl);
  460. seq_printf(s, "\n\t %-20s %s", "yh_coef",
  461. hqvdp_dbg_get_lut(c->hvsrc.yh_coef));
  462. seq_printf(s, "\n\t %-20s %s", "ch_coef",
  463. hqvdp_dbg_get_lut(c->hvsrc.ch_coef));
  464. seq_printf(s, "\n\t %-20s %s", "yv_coef",
  465. hqvdp_dbg_get_lut(c->hvsrc.yv_coef));
  466. seq_printf(s, "\n\t %-20s %s", "cv_coef",
  467. hqvdp_dbg_get_lut(c->hvsrc.cv_coef));
  468. seq_printf(s, "\n\t %-20s", "ScaleH");
  469. if (dst_w > src_w)
  470. seq_printf(s, " %d/1", dst_w / src_w);
  471. else
  472. seq_printf(s, " 1/%d", src_w / dst_w);
  473. seq_printf(s, "\n\t %-20s", "tScaleV");
  474. if (dst_h > src_h)
  475. seq_printf(s, " %d/1", dst_h / src_h);
  476. else
  477. seq_printf(s, " 1/%d", src_h / dst_h);
  478. seq_puts(s, "\n\tCSDI:");
  479. seq_printf(s, "\n\t %-20s 0x%08X\t", "Config", c->csdi.config);
  480. switch (c->csdi.config) {
  481. case CSDI_CONFIG_PROG:
  482. seq_puts(s, "Bypass");
  483. break;
  484. case CSDI_CONFIG_INTER_DIR:
  485. seq_puts(s, "Deinterlace, directional");
  486. break;
  487. default:
  488. seq_puts(s, "<UNKNOWN>");
  489. break;
  490. }
  491. seq_printf(s, "\n\t %-20s 0x%08X", "Config2", c->csdi.config2);
  492. seq_printf(s, "\n\t %-20s 0x%08X", "DcdiConfig", c->csdi.dcdi_config);
  493. }
  494. static int hqvdp_dbg_show(struct seq_file *s, void *data)
  495. {
  496. struct drm_info_node *node = s->private;
  497. struct sti_hqvdp *hqvdp = (struct sti_hqvdp *)node->info_ent->data;
  498. int cmd, cmd_offset, infoxp70;
  499. void *virt;
  500. seq_printf(s, "%s: (vaddr = 0x%p)",
  501. sti_plane_to_str(&hqvdp->plane), hqvdp->regs);
  502. DBGFS_DUMP(HQVDP_MBX_IRQ_TO_XP70);
  503. DBGFS_DUMP(HQVDP_MBX_INFO_HOST);
  504. DBGFS_DUMP(HQVDP_MBX_IRQ_TO_HOST);
  505. DBGFS_DUMP(HQVDP_MBX_INFO_XP70);
  506. infoxp70 = readl(hqvdp->regs + HQVDP_MBX_INFO_XP70);
  507. seq_puts(s, "\tFirmware state: ");
  508. if (infoxp70 & INFO_XP70_FW_READY)
  509. seq_puts(s, "idle and ready");
  510. else if (infoxp70 & INFO_XP70_FW_PROCESSING)
  511. seq_puts(s, "processing a picture");
  512. else if (infoxp70 & INFO_XP70_FW_INITQUEUES)
  513. seq_puts(s, "programming queues");
  514. else
  515. seq_puts(s, "NOT READY");
  516. DBGFS_DUMP(HQVDP_MBX_SW_RESET_CTRL);
  517. DBGFS_DUMP(HQVDP_MBX_STARTUP_CTRL1);
  518. if (readl(hqvdp->regs + HQVDP_MBX_STARTUP_CTRL1)
  519. & STARTUP_CTRL1_RST_DONE)
  520. seq_puts(s, "\tReset is done");
  521. else
  522. seq_puts(s, "\tReset is NOT done");
  523. DBGFS_DUMP(HQVDP_MBX_STARTUP_CTRL2);
  524. if (readl(hqvdp->regs + HQVDP_MBX_STARTUP_CTRL2)
  525. & STARTUP_CTRL2_FETCH_EN)
  526. seq_puts(s, "\tFetch is enabled");
  527. else
  528. seq_puts(s, "\tFetch is NOT enabled");
  529. DBGFS_DUMP(HQVDP_MBX_GP_STATUS);
  530. DBGFS_DUMP(HQVDP_MBX_NEXT_CMD);
  531. DBGFS_DUMP(HQVDP_MBX_CURRENT_CMD);
  532. DBGFS_DUMP(HQVDP_MBX_SOFT_VSYNC);
  533. if (!(readl(hqvdp->regs + HQVDP_MBX_SOFT_VSYNC) & 3))
  534. seq_puts(s, "\tHW Vsync");
  535. else
  536. seq_puts(s, "\tSW Vsync ?!?!");
  537. /* Last command */
  538. cmd = readl(hqvdp->regs + HQVDP_MBX_CURRENT_CMD);
  539. cmd_offset = sti_hqvdp_get_curr_cmd(hqvdp);
  540. if (cmd_offset == -1) {
  541. seq_puts(s, "\n\n Last command: unknown");
  542. } else {
  543. virt = hqvdp->hqvdp_cmd + cmd_offset;
  544. seq_printf(s, "\n\n Last command: address @ 0x%x (0x%p)",
  545. cmd, virt);
  546. hqvdp_dbg_dump_cmd(s, (struct sti_hqvdp_cmd *)virt);
  547. }
  548. /* Next command */
  549. cmd = readl(hqvdp->regs + HQVDP_MBX_NEXT_CMD);
  550. cmd_offset = sti_hqvdp_get_next_cmd(hqvdp);
  551. if (cmd_offset == -1) {
  552. seq_puts(s, "\n\n Next command: unknown");
  553. } else {
  554. virt = hqvdp->hqvdp_cmd + cmd_offset;
  555. seq_printf(s, "\n\n Next command address: @ 0x%x (0x%p)",
  556. cmd, virt);
  557. hqvdp_dbg_dump_cmd(s, (struct sti_hqvdp_cmd *)virt);
  558. }
  559. seq_puts(s, "\n");
  560. return 0;
  561. }
  562. static struct drm_info_list hqvdp_debugfs_files[] = {
  563. { "hqvdp", hqvdp_dbg_show, 0, NULL },
  564. };
  565. static int hqvdp_debugfs_init(struct sti_hqvdp *hqvdp, struct drm_minor *minor)
  566. {
  567. unsigned int i;
  568. for (i = 0; i < ARRAY_SIZE(hqvdp_debugfs_files); i++)
  569. hqvdp_debugfs_files[i].data = hqvdp;
  570. return drm_debugfs_create_files(hqvdp_debugfs_files,
  571. ARRAY_SIZE(hqvdp_debugfs_files),
  572. minor->debugfs_root, minor);
  573. }
  574. /**
  575. * sti_hqvdp_update_hvsrc
  576. * @orient: horizontal or vertical
  577. * @scale: scaling/zoom factor
  578. * @hvsrc: the structure containing the LUT coef
  579. *
  580. * Update the Y and C Lut coef, as well as the shift param
  581. *
  582. * RETURNS:
  583. * None.
  584. */
  585. static void sti_hqvdp_update_hvsrc(enum sti_hvsrc_orient orient, int scale,
  586. struct sti_hqvdp_hvsrc *hvsrc)
  587. {
  588. const int *coef_c, *coef_y;
  589. int shift_c, shift_y;
  590. /* Get the appropriate coef tables */
  591. if (scale < SCALE_MAX_FOR_LEG_LUT_F) {
  592. coef_y = coef_lut_f_y_legacy;
  593. coef_c = coef_lut_f_c_legacy;
  594. shift_y = SHIFT_LUT_F_Y_LEGACY;
  595. shift_c = SHIFT_LUT_F_C_LEGACY;
  596. } else if (scale < SCALE_MAX_FOR_LEG_LUT_E) {
  597. coef_y = coef_lut_e_y_legacy;
  598. coef_c = coef_lut_e_c_legacy;
  599. shift_y = SHIFT_LUT_E_Y_LEGACY;
  600. shift_c = SHIFT_LUT_E_C_LEGACY;
  601. } else if (scale < SCALE_MAX_FOR_LEG_LUT_D) {
  602. coef_y = coef_lut_d_y_legacy;
  603. coef_c = coef_lut_d_c_legacy;
  604. shift_y = SHIFT_LUT_D_Y_LEGACY;
  605. shift_c = SHIFT_LUT_D_C_LEGACY;
  606. } else if (scale < SCALE_MAX_FOR_LEG_LUT_C) {
  607. coef_y = coef_lut_c_y_legacy;
  608. coef_c = coef_lut_c_c_legacy;
  609. shift_y = SHIFT_LUT_C_Y_LEGACY;
  610. shift_c = SHIFT_LUT_C_C_LEGACY;
  611. } else if (scale == SCALE_MAX_FOR_LEG_LUT_C) {
  612. coef_y = coef_c = coef_lut_b;
  613. shift_y = shift_c = SHIFT_LUT_B;
  614. } else {
  615. coef_y = coef_c = coef_lut_a_legacy;
  616. shift_y = shift_c = SHIFT_LUT_A_LEGACY;
  617. }
  618. if (orient == HVSRC_HORI) {
  619. hvsrc->hori_shift = (shift_c << 16) | shift_y;
  620. memcpy(hvsrc->yh_coef, coef_y, sizeof(hvsrc->yh_coef));
  621. memcpy(hvsrc->ch_coef, coef_c, sizeof(hvsrc->ch_coef));
  622. } else {
  623. hvsrc->vert_shift = (shift_c << 16) | shift_y;
  624. memcpy(hvsrc->yv_coef, coef_y, sizeof(hvsrc->yv_coef));
  625. memcpy(hvsrc->cv_coef, coef_c, sizeof(hvsrc->cv_coef));
  626. }
  627. }
  628. /**
  629. * sti_hqvdp_check_hw_scaling
  630. * @hqvdp: hqvdp pointer
  631. * @mode: display mode with timing constraints
  632. * @src_w: source width
  633. * @src_h: source height
  634. * @dst_w: destination width
  635. * @dst_h: destination height
  636. *
  637. * Check if the HW is able to perform the scaling request
  638. * The firmware scaling limitation is "CEIL(1/Zy) <= FLOOR(LFW)" where:
  639. * Zy = OutputHeight / InputHeight
  640. * LFW = (Tx * IPClock) / (MaxNbCycles * Cp)
  641. * Tx : Total video mode horizontal resolution
  642. * IPClock : HQVDP IP clock (Mhz)
  643. * MaxNbCycles: max(InputWidth, OutputWidth)
  644. * Cp: Video mode pixel clock (Mhz)
  645. *
  646. * RETURNS:
  647. * True if the HW can scale.
  648. */
  649. static bool sti_hqvdp_check_hw_scaling(struct sti_hqvdp *hqvdp,
  650. struct drm_display_mode *mode,
  651. int src_w, int src_h,
  652. int dst_w, int dst_h)
  653. {
  654. unsigned long lfw;
  655. unsigned int inv_zy;
  656. lfw = mode->htotal * (clk_get_rate(hqvdp->clk) / 1000000);
  657. lfw /= max(src_w, dst_w) * mode->clock / 1000;
  658. inv_zy = DIV_ROUND_UP(src_h, dst_h);
  659. return (inv_zy <= lfw) ? true : false;
  660. }
  661. /**
  662. * sti_hqvdp_disable
  663. * @hqvdp: hqvdp pointer
  664. *
  665. * Disables the HQVDP plane
  666. */
  667. static void sti_hqvdp_disable(struct sti_hqvdp *hqvdp)
  668. {
  669. int i;
  670. DRM_DEBUG_DRIVER("%s\n", sti_plane_to_str(&hqvdp->plane));
  671. /* Unregister VTG Vsync callback */
  672. if (sti_vtg_unregister_client(hqvdp->vtg, &hqvdp->vtg_nb))
  673. DRM_DEBUG_DRIVER("Warning: cannot unregister VTG notifier\n");
  674. /* Set next cmd to NULL */
  675. writel(0, hqvdp->regs + HQVDP_MBX_NEXT_CMD);
  676. for (i = 0; i < POLL_MAX_ATTEMPT; i++) {
  677. if (readl(hqvdp->regs + HQVDP_MBX_INFO_XP70)
  678. & INFO_XP70_FW_READY)
  679. break;
  680. msleep(POLL_DELAY_MS);
  681. }
  682. /* VTG can stop now */
  683. clk_disable_unprepare(hqvdp->clk_pix_main);
  684. if (i == POLL_MAX_ATTEMPT)
  685. DRM_ERROR("XP70 could not revert to idle\n");
  686. hqvdp->plane.status = STI_PLANE_DISABLED;
  687. hqvdp->vtg_registered = false;
  688. }
  689. /**
  690. * sti_vdp_vtg_cb
  691. * @nb: notifier block
  692. * @evt: event message
  693. * @data: private data
  694. *
  695. * Handle VTG Vsync event, display pending bottom field
  696. *
  697. * RETURNS:
  698. * 0 on success.
  699. */
  700. static int sti_hqvdp_vtg_cb(struct notifier_block *nb, unsigned long evt, void *data)
  701. {
  702. struct sti_hqvdp *hqvdp = container_of(nb, struct sti_hqvdp, vtg_nb);
  703. int btm_cmd_offset, top_cmd_offest;
  704. struct sti_hqvdp_cmd *btm_cmd, *top_cmd;
  705. if ((evt != VTG_TOP_FIELD_EVENT) && (evt != VTG_BOTTOM_FIELD_EVENT)) {
  706. DRM_DEBUG_DRIVER("Unknown event\n");
  707. return 0;
  708. }
  709. if (hqvdp->plane.status == STI_PLANE_FLUSHING) {
  710. /* disable need to be synchronize on vsync event */
  711. DRM_DEBUG_DRIVER("Vsync event received => disable %s\n",
  712. sti_plane_to_str(&hqvdp->plane));
  713. sti_hqvdp_disable(hqvdp);
  714. }
  715. if (hqvdp->btm_field_pending) {
  716. /* Create the btm field command from the current one */
  717. btm_cmd_offset = sti_hqvdp_get_free_cmd(hqvdp);
  718. top_cmd_offest = sti_hqvdp_get_curr_cmd(hqvdp);
  719. if ((btm_cmd_offset == -1) || (top_cmd_offest == -1)) {
  720. DRM_DEBUG_DRIVER("Warning: no cmd, will skip field\n");
  721. return -EBUSY;
  722. }
  723. btm_cmd = hqvdp->hqvdp_cmd + btm_cmd_offset;
  724. top_cmd = hqvdp->hqvdp_cmd + top_cmd_offest;
  725. memcpy(btm_cmd, top_cmd, sizeof(*btm_cmd));
  726. btm_cmd->top.config = TOP_CONFIG_INTER_BTM;
  727. btm_cmd->top.current_luma +=
  728. btm_cmd->top.luma_src_pitch / 2;
  729. btm_cmd->top.current_chroma +=
  730. btm_cmd->top.chroma_src_pitch / 2;
  731. /* Post the command to mailbox */
  732. writel(hqvdp->hqvdp_cmd_paddr + btm_cmd_offset,
  733. hqvdp->regs + HQVDP_MBX_NEXT_CMD);
  734. hqvdp->btm_field_pending = false;
  735. dev_dbg(hqvdp->dev, "%s Posted command:0x%x\n",
  736. __func__, hqvdp->hqvdp_cmd_paddr);
  737. sti_plane_update_fps(&hqvdp->plane, false, true);
  738. }
  739. return 0;
  740. }
  741. static void sti_hqvdp_init(struct sti_hqvdp *hqvdp)
  742. {
  743. int size;
  744. dma_addr_t dma_addr;
  745. hqvdp->vtg_nb.notifier_call = sti_hqvdp_vtg_cb;
  746. /* Allocate memory for the VDP commands */
  747. size = NB_VDP_CMD * sizeof(struct sti_hqvdp_cmd);
  748. hqvdp->hqvdp_cmd = dma_alloc_wc(hqvdp->dev, size,
  749. &dma_addr,
  750. GFP_KERNEL | GFP_DMA);
  751. if (!hqvdp->hqvdp_cmd) {
  752. DRM_ERROR("Failed to allocate memory for VDP cmd\n");
  753. return;
  754. }
  755. hqvdp->hqvdp_cmd_paddr = (u32)dma_addr;
  756. memset(hqvdp->hqvdp_cmd, 0, size);
  757. }
  758. static void sti_hqvdp_init_plugs(struct sti_hqvdp *hqvdp)
  759. {
  760. /* Configure Plugs (same for RD & WR) */
  761. writel(PLUG_PAGE_SIZE_256, hqvdp->regs + HQVDP_RD_PLUG_PAGE_SIZE);
  762. writel(PLUG_MIN_OPC_8, hqvdp->regs + HQVDP_RD_PLUG_MIN_OPC);
  763. writel(PLUG_MAX_OPC_64, hqvdp->regs + HQVDP_RD_PLUG_MAX_OPC);
  764. writel(PLUG_MAX_CHK_2X, hqvdp->regs + HQVDP_RD_PLUG_MAX_CHK);
  765. writel(PLUG_MAX_MSG_1X, hqvdp->regs + HQVDP_RD_PLUG_MAX_MSG);
  766. writel(PLUG_MIN_SPACE_1, hqvdp->regs + HQVDP_RD_PLUG_MIN_SPACE);
  767. writel(PLUG_CONTROL_ENABLE, hqvdp->regs + HQVDP_RD_PLUG_CONTROL);
  768. writel(PLUG_PAGE_SIZE_256, hqvdp->regs + HQVDP_WR_PLUG_PAGE_SIZE);
  769. writel(PLUG_MIN_OPC_8, hqvdp->regs + HQVDP_WR_PLUG_MIN_OPC);
  770. writel(PLUG_MAX_OPC_64, hqvdp->regs + HQVDP_WR_PLUG_MAX_OPC);
  771. writel(PLUG_MAX_CHK_2X, hqvdp->regs + HQVDP_WR_PLUG_MAX_CHK);
  772. writel(PLUG_MAX_MSG_1X, hqvdp->regs + HQVDP_WR_PLUG_MAX_MSG);
  773. writel(PLUG_MIN_SPACE_1, hqvdp->regs + HQVDP_WR_PLUG_MIN_SPACE);
  774. writel(PLUG_CONTROL_ENABLE, hqvdp->regs + HQVDP_WR_PLUG_CONTROL);
  775. }
  776. /**
  777. * sti_hqvdp_start_xp70
  778. * @hqvdp: hqvdp pointer
  779. *
  780. * Run the xP70 initialization sequence
  781. */
  782. static void sti_hqvdp_start_xp70(struct sti_hqvdp *hqvdp)
  783. {
  784. const struct firmware *firmware;
  785. u32 *fw_rd_plug, *fw_wr_plug, *fw_pmem, *fw_dmem;
  786. u8 *data;
  787. int i;
  788. struct fw_header {
  789. int rd_size;
  790. int wr_size;
  791. int pmem_size;
  792. int dmem_size;
  793. } *header;
  794. DRM_DEBUG_DRIVER("\n");
  795. if (hqvdp->xp70_initialized) {
  796. DRM_DEBUG_DRIVER("HQVDP XP70 already initialized\n");
  797. return;
  798. }
  799. /* Request firmware */
  800. if (request_firmware(&firmware, HQVDP_FMW_NAME, hqvdp->dev)) {
  801. DRM_ERROR("Can't get HQVDP firmware\n");
  802. return;
  803. }
  804. /* Check firmware parts */
  805. if (!firmware) {
  806. DRM_ERROR("Firmware not available\n");
  807. return;
  808. }
  809. header = (struct fw_header *)firmware->data;
  810. if (firmware->size < sizeof(*header)) {
  811. DRM_ERROR("Invalid firmware size (%d)\n", firmware->size);
  812. goto out;
  813. }
  814. if ((sizeof(*header) + header->rd_size + header->wr_size +
  815. header->pmem_size + header->dmem_size) != firmware->size) {
  816. DRM_ERROR("Invalid fmw structure (%d+%d+%d+%d+%d != %d)\n",
  817. sizeof(*header), header->rd_size, header->wr_size,
  818. header->pmem_size, header->dmem_size,
  819. firmware->size);
  820. goto out;
  821. }
  822. data = (u8 *)firmware->data;
  823. data += sizeof(*header);
  824. fw_rd_plug = (void *)data;
  825. data += header->rd_size;
  826. fw_wr_plug = (void *)data;
  827. data += header->wr_size;
  828. fw_pmem = (void *)data;
  829. data += header->pmem_size;
  830. fw_dmem = (void *)data;
  831. /* Enable clock */
  832. if (clk_prepare_enable(hqvdp->clk))
  833. DRM_ERROR("Failed to prepare/enable HQVDP clk\n");
  834. /* Reset */
  835. writel(SW_RESET_CTRL_FULL, hqvdp->regs + HQVDP_MBX_SW_RESET_CTRL);
  836. for (i = 0; i < POLL_MAX_ATTEMPT; i++) {
  837. if (readl(hqvdp->regs + HQVDP_MBX_STARTUP_CTRL1)
  838. & STARTUP_CTRL1_RST_DONE)
  839. break;
  840. msleep(POLL_DELAY_MS);
  841. }
  842. if (i == POLL_MAX_ATTEMPT) {
  843. DRM_ERROR("Could not reset\n");
  844. goto out;
  845. }
  846. /* Init Read & Write plugs */
  847. for (i = 0; i < header->rd_size / 4; i++)
  848. writel(fw_rd_plug[i], hqvdp->regs + HQVDP_RD_PLUG + i * 4);
  849. for (i = 0; i < header->wr_size / 4; i++)
  850. writel(fw_wr_plug[i], hqvdp->regs + HQVDP_WR_PLUG + i * 4);
  851. sti_hqvdp_init_plugs(hqvdp);
  852. /* Authorize Idle Mode */
  853. writel(STARTUP_CTRL1_AUTH_IDLE, hqvdp->regs + HQVDP_MBX_STARTUP_CTRL1);
  854. /* Prevent VTG interruption during the boot */
  855. writel(SOFT_VSYNC_SW_CTRL_IRQ, hqvdp->regs + HQVDP_MBX_SOFT_VSYNC);
  856. writel(0, hqvdp->regs + HQVDP_MBX_NEXT_CMD);
  857. /* Download PMEM & DMEM */
  858. for (i = 0; i < header->pmem_size / 4; i++)
  859. writel(fw_pmem[i], hqvdp->regs + HQVDP_PMEM + i * 4);
  860. for (i = 0; i < header->dmem_size / 4; i++)
  861. writel(fw_dmem[i], hqvdp->regs + HQVDP_DMEM + i * 4);
  862. /* Enable fetch */
  863. writel(STARTUP_CTRL2_FETCH_EN, hqvdp->regs + HQVDP_MBX_STARTUP_CTRL2);
  864. /* Wait end of boot */
  865. for (i = 0; i < POLL_MAX_ATTEMPT; i++) {
  866. if (readl(hqvdp->regs + HQVDP_MBX_INFO_XP70)
  867. & INFO_XP70_FW_READY)
  868. break;
  869. msleep(POLL_DELAY_MS);
  870. }
  871. if (i == POLL_MAX_ATTEMPT) {
  872. DRM_ERROR("Could not boot\n");
  873. goto out;
  874. }
  875. /* Launch Vsync */
  876. writel(SOFT_VSYNC_HW, hqvdp->regs + HQVDP_MBX_SOFT_VSYNC);
  877. DRM_INFO("HQVDP XP70 initialized\n");
  878. hqvdp->xp70_initialized = true;
  879. out:
  880. release_firmware(firmware);
  881. }
  882. static int sti_hqvdp_atomic_check(struct drm_plane *drm_plane,
  883. struct drm_plane_state *state)
  884. {
  885. struct sti_plane *plane = to_sti_plane(drm_plane);
  886. struct sti_hqvdp *hqvdp = to_sti_hqvdp(plane);
  887. struct drm_crtc *crtc = state->crtc;
  888. struct drm_framebuffer *fb = state->fb;
  889. struct drm_crtc_state *crtc_state;
  890. struct drm_display_mode *mode;
  891. int dst_x, dst_y, dst_w, dst_h;
  892. int src_x, src_y, src_w, src_h;
  893. /* no need for further checks if the plane is being disabled */
  894. if (!crtc || !fb)
  895. return 0;
  896. crtc_state = drm_atomic_get_crtc_state(state->state, crtc);
  897. mode = &crtc_state->mode;
  898. dst_x = state->crtc_x;
  899. dst_y = state->crtc_y;
  900. dst_w = clamp_val(state->crtc_w, 0, mode->hdisplay - dst_x);
  901. dst_h = clamp_val(state->crtc_h, 0, mode->vdisplay - dst_y);
  902. /* src_x are in 16.16 format */
  903. src_x = state->src_x >> 16;
  904. src_y = state->src_y >> 16;
  905. src_w = state->src_w >> 16;
  906. src_h = state->src_h >> 16;
  907. if (!sti_hqvdp_check_hw_scaling(hqvdp, mode,
  908. src_w, src_h,
  909. dst_w, dst_h)) {
  910. DRM_ERROR("Scaling beyond HW capabilities\n");
  911. return -EINVAL;
  912. }
  913. if (!drm_fb_cma_get_gem_obj(fb, 0)) {
  914. DRM_ERROR("Can't get CMA GEM object for fb\n");
  915. return -EINVAL;
  916. }
  917. /*
  918. * Input / output size
  919. * Align to upper even value
  920. */
  921. dst_w = ALIGN(dst_w, 2);
  922. dst_h = ALIGN(dst_h, 2);
  923. if ((src_w > MAX_WIDTH) || (src_w < MIN_WIDTH) ||
  924. (src_h > MAX_HEIGHT) || (src_h < MIN_HEIGHT) ||
  925. (dst_w > MAX_WIDTH) || (dst_w < MIN_WIDTH) ||
  926. (dst_h > MAX_HEIGHT) || (dst_h < MIN_HEIGHT)) {
  927. DRM_ERROR("Invalid in/out size %dx%d -> %dx%d\n",
  928. src_w, src_h,
  929. dst_w, dst_h);
  930. return -EINVAL;
  931. }
  932. if (!hqvdp->xp70_initialized)
  933. /* Start HQVDP XP70 coprocessor */
  934. sti_hqvdp_start_xp70(hqvdp);
  935. if (!hqvdp->vtg_registered) {
  936. /* Prevent VTG shutdown */
  937. if (clk_prepare_enable(hqvdp->clk_pix_main)) {
  938. DRM_ERROR("Failed to prepare/enable pix main clk\n");
  939. return -EINVAL;
  940. }
  941. /* Register VTG Vsync callback to handle bottom fields */
  942. if (sti_vtg_register_client(hqvdp->vtg,
  943. &hqvdp->vtg_nb,
  944. crtc)) {
  945. DRM_ERROR("Cannot register VTG notifier\n");
  946. return -EINVAL;
  947. }
  948. hqvdp->vtg_registered = true;
  949. }
  950. DRM_DEBUG_KMS("CRTC:%d (%s) drm plane:%d (%s)\n",
  951. crtc->base.id, sti_mixer_to_str(to_sti_mixer(crtc)),
  952. drm_plane->base.id, sti_plane_to_str(plane));
  953. DRM_DEBUG_KMS("%s dst=(%dx%d)@(%d,%d) - src=(%dx%d)@(%d,%d)\n",
  954. sti_plane_to_str(plane),
  955. dst_w, dst_h, dst_x, dst_y,
  956. src_w, src_h, src_x, src_y);
  957. return 0;
  958. }
  959. static void sti_hqvdp_atomic_update(struct drm_plane *drm_plane,
  960. struct drm_plane_state *oldstate)
  961. {
  962. struct drm_plane_state *state = drm_plane->state;
  963. struct sti_plane *plane = to_sti_plane(drm_plane);
  964. struct sti_hqvdp *hqvdp = to_sti_hqvdp(plane);
  965. struct drm_crtc *crtc = state->crtc;
  966. struct drm_framebuffer *fb = state->fb;
  967. struct drm_display_mode *mode;
  968. int dst_x, dst_y, dst_w, dst_h;
  969. int src_x, src_y, src_w, src_h;
  970. struct drm_gem_cma_object *cma_obj;
  971. struct sti_hqvdp_cmd *cmd;
  972. int scale_h, scale_v;
  973. int cmd_offset;
  974. if (!crtc || !fb)
  975. return;
  976. if ((oldstate->fb == state->fb) &&
  977. (oldstate->crtc_x == state->crtc_x) &&
  978. (oldstate->crtc_y == state->crtc_y) &&
  979. (oldstate->crtc_w == state->crtc_w) &&
  980. (oldstate->crtc_h == state->crtc_h) &&
  981. (oldstate->src_x == state->src_x) &&
  982. (oldstate->src_y == state->src_y) &&
  983. (oldstate->src_w == state->src_w) &&
  984. (oldstate->src_h == state->src_h)) {
  985. /* No change since last update, do not post cmd */
  986. DRM_DEBUG_DRIVER("No change, not posting cmd\n");
  987. plane->status = STI_PLANE_UPDATED;
  988. return;
  989. }
  990. mode = &crtc->mode;
  991. dst_x = state->crtc_x;
  992. dst_y = state->crtc_y;
  993. dst_w = clamp_val(state->crtc_w, 0, mode->hdisplay - dst_x);
  994. dst_h = clamp_val(state->crtc_h, 0, mode->vdisplay - dst_y);
  995. /* src_x are in 16.16 format */
  996. src_x = state->src_x >> 16;
  997. src_y = state->src_y >> 16;
  998. src_w = state->src_w >> 16;
  999. src_h = state->src_h >> 16;
  1000. cmd_offset = sti_hqvdp_get_free_cmd(hqvdp);
  1001. if (cmd_offset == -1) {
  1002. DRM_DEBUG_DRIVER("Warning: no cmd, will skip frame\n");
  1003. return;
  1004. }
  1005. cmd = hqvdp->hqvdp_cmd + cmd_offset;
  1006. /* Static parameters, defaulting to progressive mode */
  1007. cmd->top.config = TOP_CONFIG_PROGRESSIVE;
  1008. cmd->top.mem_format = TOP_MEM_FORMAT_DFLT;
  1009. cmd->hvsrc.param_ctrl = HVSRC_PARAM_CTRL_DFLT;
  1010. cmd->csdi.config = CSDI_CONFIG_PROG;
  1011. /* VC1RE, FMD bypassed : keep everything set to 0
  1012. * IQI/P2I bypassed */
  1013. cmd->iqi.config = IQI_CONFIG_DFLT;
  1014. cmd->iqi.con_bri = IQI_CON_BRI_DFLT;
  1015. cmd->iqi.sat_gain = IQI_SAT_GAIN_DFLT;
  1016. cmd->iqi.pxf_conf = IQI_PXF_CONF_DFLT;
  1017. cma_obj = drm_fb_cma_get_gem_obj(fb, 0);
  1018. DRM_DEBUG_DRIVER("drm FB:%d format:%.4s phys@:0x%lx\n", fb->base.id,
  1019. (char *)&fb->format->format,
  1020. (unsigned long)cma_obj->paddr);
  1021. /* Buffer planes address */
  1022. cmd->top.current_luma = (u32)cma_obj->paddr + fb->offsets[0];
  1023. cmd->top.current_chroma = (u32)cma_obj->paddr + fb->offsets[1];
  1024. /* Pitches */
  1025. cmd->top.luma_processed_pitch = fb->pitches[0];
  1026. cmd->top.luma_src_pitch = fb->pitches[0];
  1027. cmd->top.chroma_processed_pitch = fb->pitches[1];
  1028. cmd->top.chroma_src_pitch = fb->pitches[1];
  1029. /* Input / output size
  1030. * Align to upper even value */
  1031. dst_w = ALIGN(dst_w, 2);
  1032. dst_h = ALIGN(dst_h, 2);
  1033. cmd->top.input_viewport_size = src_h << 16 | src_w;
  1034. cmd->top.input_frame_size = src_h << 16 | src_w;
  1035. cmd->hvsrc.output_picture_size = dst_h << 16 | dst_w;
  1036. cmd->top.input_viewport_ori = src_y << 16 | src_x;
  1037. /* Handle interlaced */
  1038. if (fb->flags & DRM_MODE_FB_INTERLACED) {
  1039. /* Top field to display */
  1040. cmd->top.config = TOP_CONFIG_INTER_TOP;
  1041. /* Update pitches and vert size */
  1042. cmd->top.input_frame_size = (src_h / 2) << 16 | src_w;
  1043. cmd->top.luma_processed_pitch *= 2;
  1044. cmd->top.luma_src_pitch *= 2;
  1045. cmd->top.chroma_processed_pitch *= 2;
  1046. cmd->top.chroma_src_pitch *= 2;
  1047. /* Enable directional deinterlacing processing */
  1048. cmd->csdi.config = CSDI_CONFIG_INTER_DIR;
  1049. cmd->csdi.config2 = CSDI_CONFIG2_DFLT;
  1050. cmd->csdi.dcdi_config = CSDI_DCDI_CONFIG_DFLT;
  1051. }
  1052. /* Update hvsrc lut coef */
  1053. scale_h = SCALE_FACTOR * dst_w / src_w;
  1054. sti_hqvdp_update_hvsrc(HVSRC_HORI, scale_h, &cmd->hvsrc);
  1055. scale_v = SCALE_FACTOR * dst_h / src_h;
  1056. sti_hqvdp_update_hvsrc(HVSRC_VERT, scale_v, &cmd->hvsrc);
  1057. writel(hqvdp->hqvdp_cmd_paddr + cmd_offset,
  1058. hqvdp->regs + HQVDP_MBX_NEXT_CMD);
  1059. /* Interlaced : get ready to display the bottom field at next Vsync */
  1060. if (fb->flags & DRM_MODE_FB_INTERLACED)
  1061. hqvdp->btm_field_pending = true;
  1062. dev_dbg(hqvdp->dev, "%s Posted command:0x%x\n",
  1063. __func__, hqvdp->hqvdp_cmd_paddr + cmd_offset);
  1064. sti_plane_update_fps(plane, true, true);
  1065. plane->status = STI_PLANE_UPDATED;
  1066. }
  1067. static void sti_hqvdp_atomic_disable(struct drm_plane *drm_plane,
  1068. struct drm_plane_state *oldstate)
  1069. {
  1070. struct sti_plane *plane = to_sti_plane(drm_plane);
  1071. if (!oldstate->crtc) {
  1072. DRM_DEBUG_DRIVER("drm plane:%d not enabled\n",
  1073. drm_plane->base.id);
  1074. return;
  1075. }
  1076. DRM_DEBUG_DRIVER("CRTC:%d (%s) drm plane:%d (%s)\n",
  1077. oldstate->crtc->base.id,
  1078. sti_mixer_to_str(to_sti_mixer(oldstate->crtc)),
  1079. drm_plane->base.id, sti_plane_to_str(plane));
  1080. plane->status = STI_PLANE_DISABLING;
  1081. }
  1082. static const struct drm_plane_helper_funcs sti_hqvdp_helpers_funcs = {
  1083. .atomic_check = sti_hqvdp_atomic_check,
  1084. .atomic_update = sti_hqvdp_atomic_update,
  1085. .atomic_disable = sti_hqvdp_atomic_disable,
  1086. };
  1087. static void sti_hqvdp_destroy(struct drm_plane *drm_plane)
  1088. {
  1089. DRM_DEBUG_DRIVER("\n");
  1090. drm_plane_helper_disable(drm_plane);
  1091. drm_plane_cleanup(drm_plane);
  1092. }
  1093. static int sti_hqvdp_late_register(struct drm_plane *drm_plane)
  1094. {
  1095. struct sti_plane *plane = to_sti_plane(drm_plane);
  1096. struct sti_hqvdp *hqvdp = to_sti_hqvdp(plane);
  1097. return hqvdp_debugfs_init(hqvdp, drm_plane->dev->primary);
  1098. }
  1099. static const struct drm_plane_funcs sti_hqvdp_plane_helpers_funcs = {
  1100. .update_plane = drm_atomic_helper_update_plane,
  1101. .disable_plane = drm_atomic_helper_disable_plane,
  1102. .destroy = sti_hqvdp_destroy,
  1103. .set_property = drm_atomic_helper_plane_set_property,
  1104. .reset = sti_plane_reset,
  1105. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  1106. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  1107. .late_register = sti_hqvdp_late_register,
  1108. };
  1109. static struct drm_plane *sti_hqvdp_create(struct drm_device *drm_dev,
  1110. struct device *dev, int desc)
  1111. {
  1112. struct sti_hqvdp *hqvdp = dev_get_drvdata(dev);
  1113. int res;
  1114. hqvdp->plane.desc = desc;
  1115. hqvdp->plane.status = STI_PLANE_DISABLED;
  1116. sti_hqvdp_init(hqvdp);
  1117. res = drm_universal_plane_init(drm_dev, &hqvdp->plane.drm_plane, 1,
  1118. &sti_hqvdp_plane_helpers_funcs,
  1119. hqvdp_supported_formats,
  1120. ARRAY_SIZE(hqvdp_supported_formats),
  1121. DRM_PLANE_TYPE_OVERLAY, NULL);
  1122. if (res) {
  1123. DRM_ERROR("Failed to initialize universal plane\n");
  1124. return NULL;
  1125. }
  1126. drm_plane_helper_add(&hqvdp->plane.drm_plane, &sti_hqvdp_helpers_funcs);
  1127. sti_plane_init_property(&hqvdp->plane, DRM_PLANE_TYPE_OVERLAY);
  1128. return &hqvdp->plane.drm_plane;
  1129. }
  1130. static int sti_hqvdp_bind(struct device *dev, struct device *master, void *data)
  1131. {
  1132. struct sti_hqvdp *hqvdp = dev_get_drvdata(dev);
  1133. struct drm_device *drm_dev = data;
  1134. struct drm_plane *plane;
  1135. DRM_DEBUG_DRIVER("\n");
  1136. hqvdp->drm_dev = drm_dev;
  1137. /* Create HQVDP plane once xp70 is initialized */
  1138. plane = sti_hqvdp_create(drm_dev, hqvdp->dev, STI_HQVDP_0);
  1139. if (!plane)
  1140. DRM_ERROR("Can't create HQVDP plane\n");
  1141. return 0;
  1142. }
  1143. static void sti_hqvdp_unbind(struct device *dev,
  1144. struct device *master, void *data)
  1145. {
  1146. /* do nothing */
  1147. }
  1148. static const struct component_ops sti_hqvdp_ops = {
  1149. .bind = sti_hqvdp_bind,
  1150. .unbind = sti_hqvdp_unbind,
  1151. };
  1152. static int sti_hqvdp_probe(struct platform_device *pdev)
  1153. {
  1154. struct device *dev = &pdev->dev;
  1155. struct device_node *vtg_np;
  1156. struct sti_hqvdp *hqvdp;
  1157. struct resource *res;
  1158. DRM_DEBUG_DRIVER("\n");
  1159. hqvdp = devm_kzalloc(dev, sizeof(*hqvdp), GFP_KERNEL);
  1160. if (!hqvdp) {
  1161. DRM_ERROR("Failed to allocate HQVDP context\n");
  1162. return -ENOMEM;
  1163. }
  1164. hqvdp->dev = dev;
  1165. /* Get Memory resources */
  1166. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1167. if (res == NULL) {
  1168. DRM_ERROR("Get memory resource failed\n");
  1169. return -ENXIO;
  1170. }
  1171. hqvdp->regs = devm_ioremap(dev, res->start, resource_size(res));
  1172. if (hqvdp->regs == NULL) {
  1173. DRM_ERROR("Register mapping failed\n");
  1174. return -ENXIO;
  1175. }
  1176. /* Get clock resources */
  1177. hqvdp->clk = devm_clk_get(dev, "hqvdp");
  1178. hqvdp->clk_pix_main = devm_clk_get(dev, "pix_main");
  1179. if (IS_ERR(hqvdp->clk) || IS_ERR(hqvdp->clk_pix_main)) {
  1180. DRM_ERROR("Cannot get clocks\n");
  1181. return -ENXIO;
  1182. }
  1183. /* Get reset resources */
  1184. hqvdp->reset = devm_reset_control_get(dev, "hqvdp");
  1185. if (!IS_ERR(hqvdp->reset))
  1186. reset_control_deassert(hqvdp->reset);
  1187. vtg_np = of_parse_phandle(pdev->dev.of_node, "st,vtg", 0);
  1188. if (vtg_np)
  1189. hqvdp->vtg = of_vtg_find(vtg_np);
  1190. of_node_put(vtg_np);
  1191. platform_set_drvdata(pdev, hqvdp);
  1192. return component_add(&pdev->dev, &sti_hqvdp_ops);
  1193. }
  1194. static int sti_hqvdp_remove(struct platform_device *pdev)
  1195. {
  1196. component_del(&pdev->dev, &sti_hqvdp_ops);
  1197. return 0;
  1198. }
  1199. static struct of_device_id hqvdp_of_match[] = {
  1200. { .compatible = "st,stih407-hqvdp", },
  1201. { /* end node */ }
  1202. };
  1203. MODULE_DEVICE_TABLE(of, hqvdp_of_match);
  1204. struct platform_driver sti_hqvdp_driver = {
  1205. .driver = {
  1206. .name = "sti-hqvdp",
  1207. .owner = THIS_MODULE,
  1208. .of_match_table = hqvdp_of_match,
  1209. },
  1210. .probe = sti_hqvdp_probe,
  1211. .remove = sti_hqvdp_remove,
  1212. };
  1213. MODULE_AUTHOR("Benjamin Gaignard <benjamin.gaignard@st.com>");
  1214. MODULE_DESCRIPTION("STMicroelectronics SoC DRM driver");
  1215. MODULE_LICENSE("GPL");