vc4_crtc.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  1. /*
  2. * Copyright (C) 2015 Broadcom
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. /**
  9. * DOC: VC4 CRTC module
  10. *
  11. * In VC4, the Pixel Valve is what most closely corresponds to the
  12. * DRM's concept of a CRTC. The PV generates video timings from the
  13. * encoder's clock plus its configuration. It pulls scaled pixels from
  14. * the HVS at that timing, and feeds it to the encoder.
  15. *
  16. * However, the DRM CRTC also collects the configuration of all the
  17. * DRM planes attached to it. As a result, the CRTC is also
  18. * responsible for writing the display list for the HVS channel that
  19. * the CRTC will use.
  20. *
  21. * The 2835 has 3 different pixel valves. pv0 in the audio power
  22. * domain feeds DSI0 or DPI, while pv1 feeds DS1 or SMI. pv2 in the
  23. * image domain can feed either HDMI or the SDTV controller. The
  24. * pixel valve chooses from the CPRMAN clocks (HSM for HDMI, VEC for
  25. * SDTV, etc.) according to which output type is chosen in the mux.
  26. *
  27. * For power management, the pixel valve's registers are all clocked
  28. * by the AXI clock, while the timings and FIFOs make use of the
  29. * output-specific clock. Since the encoders also directly consume
  30. * the CPRMAN clocks, and know what timings they need, they are the
  31. * ones that set the clock.
  32. */
  33. #include <drm/drm_atomic.h>
  34. #include <drm/drm_atomic_helper.h>
  35. #include <drm/drm_crtc_helper.h>
  36. #include <linux/clk.h>
  37. #include <drm/drm_fb_cma_helper.h>
  38. #include <linux/component.h>
  39. #include <linux/of_device.h>
  40. #include "vc4_drv.h"
  41. #include "vc4_regs.h"
  42. struct vc4_crtc_state {
  43. struct drm_crtc_state base;
  44. /* Dlist area for this CRTC configuration. */
  45. struct drm_mm_node mm;
  46. bool feed_txp;
  47. bool txp_armed;
  48. };
  49. static inline struct vc4_crtc_state *
  50. to_vc4_crtc_state(struct drm_crtc_state *crtc_state)
  51. {
  52. return (struct vc4_crtc_state *)crtc_state;
  53. }
  54. #define CRTC_WRITE(offset, val) writel(val, vc4_crtc->regs + (offset))
  55. #define CRTC_READ(offset) readl(vc4_crtc->regs + (offset))
  56. #define CRTC_REG(reg) { reg, #reg }
  57. static const struct {
  58. u32 reg;
  59. const char *name;
  60. } crtc_regs[] = {
  61. CRTC_REG(PV_CONTROL),
  62. CRTC_REG(PV_V_CONTROL),
  63. CRTC_REG(PV_VSYNCD_EVEN),
  64. CRTC_REG(PV_HORZA),
  65. CRTC_REG(PV_HORZB),
  66. CRTC_REG(PV_VERTA),
  67. CRTC_REG(PV_VERTB),
  68. CRTC_REG(PV_VERTA_EVEN),
  69. CRTC_REG(PV_VERTB_EVEN),
  70. CRTC_REG(PV_INTEN),
  71. CRTC_REG(PV_INTSTAT),
  72. CRTC_REG(PV_STAT),
  73. CRTC_REG(PV_HACT_ACT),
  74. };
  75. static void vc4_crtc_dump_regs(struct vc4_crtc *vc4_crtc)
  76. {
  77. int i;
  78. for (i = 0; i < ARRAY_SIZE(crtc_regs); i++) {
  79. DRM_INFO("0x%04x (%s): 0x%08x\n",
  80. crtc_regs[i].reg, crtc_regs[i].name,
  81. CRTC_READ(crtc_regs[i].reg));
  82. }
  83. }
  84. #ifdef CONFIG_DEBUG_FS
  85. int vc4_crtc_debugfs_regs(struct seq_file *m, void *unused)
  86. {
  87. struct drm_info_node *node = (struct drm_info_node *)m->private;
  88. struct drm_device *dev = node->minor->dev;
  89. int crtc_index = (uintptr_t)node->info_ent->data;
  90. struct drm_crtc *crtc;
  91. struct vc4_crtc *vc4_crtc;
  92. int i;
  93. i = 0;
  94. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  95. if (i == crtc_index)
  96. break;
  97. i++;
  98. }
  99. if (!crtc)
  100. return 0;
  101. vc4_crtc = to_vc4_crtc(crtc);
  102. for (i = 0; i < ARRAY_SIZE(crtc_regs); i++) {
  103. seq_printf(m, "%s (0x%04x): 0x%08x\n",
  104. crtc_regs[i].name, crtc_regs[i].reg,
  105. CRTC_READ(crtc_regs[i].reg));
  106. }
  107. return 0;
  108. }
  109. #endif
  110. bool vc4_crtc_get_scanoutpos(struct drm_device *dev, unsigned int crtc_id,
  111. bool in_vblank_irq, int *vpos, int *hpos,
  112. ktime_t *stime, ktime_t *etime,
  113. const struct drm_display_mode *mode)
  114. {
  115. struct vc4_dev *vc4 = to_vc4_dev(dev);
  116. struct drm_crtc *crtc = drm_crtc_from_index(dev, crtc_id);
  117. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  118. u32 val;
  119. int fifo_lines;
  120. int vblank_lines;
  121. bool ret = false;
  122. /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
  123. /* Get optional system timestamp before query. */
  124. if (stime)
  125. *stime = ktime_get();
  126. /*
  127. * Read vertical scanline which is currently composed for our
  128. * pixelvalve by the HVS, and also the scaler status.
  129. */
  130. val = HVS_READ(SCALER_DISPSTATX(vc4_crtc->channel));
  131. /* Get optional system timestamp after query. */
  132. if (etime)
  133. *etime = ktime_get();
  134. /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
  135. /* Vertical position of hvs composed scanline. */
  136. *vpos = VC4_GET_FIELD(val, SCALER_DISPSTATX_LINE);
  137. *hpos = 0;
  138. if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
  139. *vpos /= 2;
  140. /* Use hpos to correct for field offset in interlaced mode. */
  141. if (VC4_GET_FIELD(val, SCALER_DISPSTATX_FRAME_COUNT) % 2)
  142. *hpos += mode->crtc_htotal / 2;
  143. }
  144. /* This is the offset we need for translating hvs -> pv scanout pos. */
  145. fifo_lines = vc4_crtc->cob_size / mode->crtc_hdisplay;
  146. if (fifo_lines > 0)
  147. ret = true;
  148. /* HVS more than fifo_lines into frame for compositing? */
  149. if (*vpos > fifo_lines) {
  150. /*
  151. * We are in active scanout and can get some meaningful results
  152. * from HVS. The actual PV scanout can not trail behind more
  153. * than fifo_lines as that is the fifo's capacity. Assume that
  154. * in active scanout the HVS and PV work in lockstep wrt. HVS
  155. * refilling the fifo and PV consuming from the fifo, ie.
  156. * whenever the PV consumes and frees up a scanline in the
  157. * fifo, the HVS will immediately refill it, therefore
  158. * incrementing vpos. Therefore we choose HVS read position -
  159. * fifo size in scanlines as a estimate of the real scanout
  160. * position of the PV.
  161. */
  162. *vpos -= fifo_lines + 1;
  163. return ret;
  164. }
  165. /*
  166. * Less: This happens when we are in vblank and the HVS, after getting
  167. * the VSTART restart signal from the PV, just started refilling its
  168. * fifo with new lines from the top-most lines of the new framebuffers.
  169. * The PV does not scan out in vblank, so does not remove lines from
  170. * the fifo, so the fifo will be full quickly and the HVS has to pause.
  171. * We can't get meaningful readings wrt. scanline position of the PV
  172. * and need to make things up in a approximative but consistent way.
  173. */
  174. vblank_lines = mode->vtotal - mode->vdisplay;
  175. if (in_vblank_irq) {
  176. /*
  177. * Assume the irq handler got called close to first
  178. * line of vblank, so PV has about a full vblank
  179. * scanlines to go, and as a base timestamp use the
  180. * one taken at entry into vblank irq handler, so it
  181. * is not affected by random delays due to lock
  182. * contention on event_lock or vblank_time lock in
  183. * the core.
  184. */
  185. *vpos = -vblank_lines;
  186. if (stime)
  187. *stime = vc4_crtc->t_vblank;
  188. if (etime)
  189. *etime = vc4_crtc->t_vblank;
  190. /*
  191. * If the HVS fifo is not yet full then we know for certain
  192. * we are at the very beginning of vblank, as the hvs just
  193. * started refilling, and the stime and etime timestamps
  194. * truly correspond to start of vblank.
  195. *
  196. * Unfortunately there's no way to report this to upper levels
  197. * and make it more useful.
  198. */
  199. } else {
  200. /*
  201. * No clue where we are inside vblank. Return a vpos of zero,
  202. * which will cause calling code to just return the etime
  203. * timestamp uncorrected. At least this is no worse than the
  204. * standard fallback.
  205. */
  206. *vpos = 0;
  207. }
  208. return ret;
  209. }
  210. static void vc4_crtc_destroy(struct drm_crtc *crtc)
  211. {
  212. drm_crtc_cleanup(crtc);
  213. }
  214. static void
  215. vc4_crtc_lut_load(struct drm_crtc *crtc)
  216. {
  217. struct drm_device *dev = crtc->dev;
  218. struct vc4_dev *vc4 = to_vc4_dev(dev);
  219. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  220. u32 i;
  221. /* The LUT memory is laid out with each HVS channel in order,
  222. * each of which takes 256 writes for R, 256 for G, then 256
  223. * for B.
  224. */
  225. HVS_WRITE(SCALER_GAMADDR,
  226. SCALER_GAMADDR_AUTOINC |
  227. (vc4_crtc->channel * 3 * crtc->gamma_size));
  228. for (i = 0; i < crtc->gamma_size; i++)
  229. HVS_WRITE(SCALER_GAMDATA, vc4_crtc->lut_r[i]);
  230. for (i = 0; i < crtc->gamma_size; i++)
  231. HVS_WRITE(SCALER_GAMDATA, vc4_crtc->lut_g[i]);
  232. for (i = 0; i < crtc->gamma_size; i++)
  233. HVS_WRITE(SCALER_GAMDATA, vc4_crtc->lut_b[i]);
  234. }
  235. static void
  236. vc4_crtc_update_gamma_lut(struct drm_crtc *crtc)
  237. {
  238. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  239. struct drm_color_lut *lut = crtc->state->gamma_lut->data;
  240. u32 length = drm_color_lut_size(crtc->state->gamma_lut);
  241. u32 i;
  242. for (i = 0; i < length; i++) {
  243. vc4_crtc->lut_r[i] = drm_color_lut_extract(lut[i].red, 8);
  244. vc4_crtc->lut_g[i] = drm_color_lut_extract(lut[i].green, 8);
  245. vc4_crtc->lut_b[i] = drm_color_lut_extract(lut[i].blue, 8);
  246. }
  247. vc4_crtc_lut_load(crtc);
  248. }
  249. static u32 vc4_get_fifo_full_level(u32 format)
  250. {
  251. static const u32 fifo_len_bytes = 64;
  252. static const u32 hvs_latency_pix = 6;
  253. switch (format) {
  254. case PV_CONTROL_FORMAT_DSIV_16:
  255. case PV_CONTROL_FORMAT_DSIC_16:
  256. return fifo_len_bytes - 2 * hvs_latency_pix;
  257. case PV_CONTROL_FORMAT_DSIV_18:
  258. return fifo_len_bytes - 14;
  259. case PV_CONTROL_FORMAT_24:
  260. case PV_CONTROL_FORMAT_DSIV_24:
  261. default:
  262. return fifo_len_bytes - 3 * hvs_latency_pix;
  263. }
  264. }
  265. /*
  266. * Returns the encoder attached to the CRTC.
  267. *
  268. * VC4 can only scan out to one encoder at a time, while the DRM core
  269. * allows drivers to push pixels to more than one encoder from the
  270. * same CRTC.
  271. */
  272. static struct drm_encoder *vc4_get_crtc_encoder(struct drm_crtc *crtc)
  273. {
  274. struct drm_connector *connector;
  275. struct drm_connector_list_iter conn_iter;
  276. drm_connector_list_iter_begin(crtc->dev, &conn_iter);
  277. drm_for_each_connector_iter(connector, &conn_iter) {
  278. if (connector->state->crtc == crtc) {
  279. drm_connector_list_iter_end(&conn_iter);
  280. return connector->encoder;
  281. }
  282. }
  283. drm_connector_list_iter_end(&conn_iter);
  284. return NULL;
  285. }
  286. static void vc4_crtc_config_pv(struct drm_crtc *crtc)
  287. {
  288. struct drm_encoder *encoder = vc4_get_crtc_encoder(crtc);
  289. struct vc4_encoder *vc4_encoder = to_vc4_encoder(encoder);
  290. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  291. struct drm_crtc_state *state = crtc->state;
  292. struct drm_display_mode *mode = &state->adjusted_mode;
  293. bool interlace = mode->flags & DRM_MODE_FLAG_INTERLACE;
  294. u32 pixel_rep = (mode->flags & DRM_MODE_FLAG_DBLCLK) ? 2 : 1;
  295. bool is_dsi = (vc4_encoder->type == VC4_ENCODER_TYPE_DSI0 ||
  296. vc4_encoder->type == VC4_ENCODER_TYPE_DSI1);
  297. u32 format = is_dsi ? PV_CONTROL_FORMAT_DSIV_24 : PV_CONTROL_FORMAT_24;
  298. /* Reset the PV fifo. */
  299. CRTC_WRITE(PV_CONTROL, 0);
  300. CRTC_WRITE(PV_CONTROL, PV_CONTROL_FIFO_CLR | PV_CONTROL_EN);
  301. CRTC_WRITE(PV_CONTROL, 0);
  302. CRTC_WRITE(PV_HORZA,
  303. VC4_SET_FIELD((mode->htotal -
  304. mode->hsync_end) * pixel_rep,
  305. PV_HORZA_HBP) |
  306. VC4_SET_FIELD((mode->hsync_end -
  307. mode->hsync_start) * pixel_rep,
  308. PV_HORZA_HSYNC));
  309. CRTC_WRITE(PV_HORZB,
  310. VC4_SET_FIELD((mode->hsync_start -
  311. mode->hdisplay) * pixel_rep,
  312. PV_HORZB_HFP) |
  313. VC4_SET_FIELD(mode->hdisplay * pixel_rep, PV_HORZB_HACTIVE));
  314. CRTC_WRITE(PV_VERTA,
  315. VC4_SET_FIELD(mode->crtc_vtotal - mode->crtc_vsync_end,
  316. PV_VERTA_VBP) |
  317. VC4_SET_FIELD(mode->crtc_vsync_end - mode->crtc_vsync_start,
  318. PV_VERTA_VSYNC));
  319. CRTC_WRITE(PV_VERTB,
  320. VC4_SET_FIELD(mode->crtc_vsync_start - mode->crtc_vdisplay,
  321. PV_VERTB_VFP) |
  322. VC4_SET_FIELD(mode->crtc_vdisplay, PV_VERTB_VACTIVE));
  323. if (interlace) {
  324. CRTC_WRITE(PV_VERTA_EVEN,
  325. VC4_SET_FIELD(mode->crtc_vtotal -
  326. mode->crtc_vsync_end - 1,
  327. PV_VERTA_VBP) |
  328. VC4_SET_FIELD(mode->crtc_vsync_end -
  329. mode->crtc_vsync_start,
  330. PV_VERTA_VSYNC));
  331. CRTC_WRITE(PV_VERTB_EVEN,
  332. VC4_SET_FIELD(mode->crtc_vsync_start -
  333. mode->crtc_vdisplay,
  334. PV_VERTB_VFP) |
  335. VC4_SET_FIELD(mode->crtc_vdisplay, PV_VERTB_VACTIVE));
  336. /* We set up first field even mode for HDMI. VEC's
  337. * NTSC mode would want first field odd instead, once
  338. * we support it (to do so, set ODD_FIRST and put the
  339. * delay in VSYNCD_EVEN instead).
  340. */
  341. CRTC_WRITE(PV_V_CONTROL,
  342. PV_VCONTROL_CONTINUOUS |
  343. (is_dsi ? PV_VCONTROL_DSI : 0) |
  344. PV_VCONTROL_INTERLACE |
  345. VC4_SET_FIELD(mode->htotal * pixel_rep / 2,
  346. PV_VCONTROL_ODD_DELAY));
  347. CRTC_WRITE(PV_VSYNCD_EVEN, 0);
  348. } else {
  349. CRTC_WRITE(PV_V_CONTROL,
  350. PV_VCONTROL_CONTINUOUS |
  351. (is_dsi ? PV_VCONTROL_DSI : 0));
  352. }
  353. CRTC_WRITE(PV_HACT_ACT, mode->hdisplay * pixel_rep);
  354. CRTC_WRITE(PV_CONTROL,
  355. VC4_SET_FIELD(format, PV_CONTROL_FORMAT) |
  356. VC4_SET_FIELD(vc4_get_fifo_full_level(format),
  357. PV_CONTROL_FIFO_LEVEL) |
  358. VC4_SET_FIELD(pixel_rep - 1, PV_CONTROL_PIXEL_REP) |
  359. PV_CONTROL_CLR_AT_START |
  360. PV_CONTROL_TRIGGER_UNDERFLOW |
  361. PV_CONTROL_WAIT_HSTART |
  362. VC4_SET_FIELD(vc4_encoder->clock_select,
  363. PV_CONTROL_CLK_SELECT) |
  364. PV_CONTROL_FIFO_CLR |
  365. PV_CONTROL_EN);
  366. }
  367. static void vc4_crtc_mode_set_nofb(struct drm_crtc *crtc)
  368. {
  369. struct drm_device *dev = crtc->dev;
  370. struct vc4_dev *vc4 = to_vc4_dev(dev);
  371. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  372. struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc->state);
  373. struct drm_display_mode *mode = &crtc->state->adjusted_mode;
  374. bool interlace = mode->flags & DRM_MODE_FLAG_INTERLACE;
  375. bool debug_dump_regs = false;
  376. if (debug_dump_regs) {
  377. DRM_INFO("CRTC %d regs before:\n", drm_crtc_index(crtc));
  378. vc4_crtc_dump_regs(vc4_crtc);
  379. }
  380. if (vc4_crtc->channel == 2) {
  381. u32 dispctrl;
  382. u32 dsp3_mux;
  383. /*
  384. * SCALER_DISPCTRL_DSP3 = X, where X < 2 means 'connect DSP3 to
  385. * FIFO X'.
  386. * SCALER_DISPCTRL_DSP3 = 3 means 'disable DSP 3'.
  387. *
  388. * DSP3 is connected to FIFO2 unless the transposer is
  389. * enabled. In this case, FIFO 2 is directly accessed by the
  390. * TXP IP, and we need to disable the FIFO2 -> pixelvalve1
  391. * route.
  392. */
  393. if (vc4_state->feed_txp)
  394. dsp3_mux = VC4_SET_FIELD(3, SCALER_DISPCTRL_DSP3_MUX);
  395. else
  396. dsp3_mux = VC4_SET_FIELD(2, SCALER_DISPCTRL_DSP3_MUX);
  397. dispctrl = HVS_READ(SCALER_DISPCTRL) &
  398. ~SCALER_DISPCTRL_DSP3_MUX_MASK;
  399. HVS_WRITE(SCALER_DISPCTRL, dispctrl | dsp3_mux);
  400. }
  401. if (!vc4_state->feed_txp)
  402. vc4_crtc_config_pv(crtc);
  403. HVS_WRITE(SCALER_DISPBKGNDX(vc4_crtc->channel),
  404. SCALER_DISPBKGND_AUTOHS |
  405. SCALER_DISPBKGND_GAMMA |
  406. (interlace ? SCALER_DISPBKGND_INTERLACE : 0));
  407. /* Reload the LUT, since the SRAMs would have been disabled if
  408. * all CRTCs had SCALER_DISPBKGND_GAMMA unset at once.
  409. */
  410. vc4_crtc_lut_load(crtc);
  411. if (debug_dump_regs) {
  412. DRM_INFO("CRTC %d regs after:\n", drm_crtc_index(crtc));
  413. vc4_crtc_dump_regs(vc4_crtc);
  414. }
  415. }
  416. static void require_hvs_enabled(struct drm_device *dev)
  417. {
  418. struct vc4_dev *vc4 = to_vc4_dev(dev);
  419. WARN_ON_ONCE((HVS_READ(SCALER_DISPCTRL) & SCALER_DISPCTRL_ENABLE) !=
  420. SCALER_DISPCTRL_ENABLE);
  421. }
  422. static void vc4_crtc_atomic_disable(struct drm_crtc *crtc,
  423. struct drm_crtc_state *old_state)
  424. {
  425. struct drm_device *dev = crtc->dev;
  426. struct vc4_dev *vc4 = to_vc4_dev(dev);
  427. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  428. u32 chan = vc4_crtc->channel;
  429. int ret;
  430. require_hvs_enabled(dev);
  431. /* Disable vblank irq handling before crtc is disabled. */
  432. drm_crtc_vblank_off(crtc);
  433. CRTC_WRITE(PV_V_CONTROL,
  434. CRTC_READ(PV_V_CONTROL) & ~PV_VCONTROL_VIDEN);
  435. ret = wait_for(!(CRTC_READ(PV_V_CONTROL) & PV_VCONTROL_VIDEN), 1);
  436. WARN_ONCE(ret, "Timeout waiting for !PV_VCONTROL_VIDEN\n");
  437. if (HVS_READ(SCALER_DISPCTRLX(chan)) &
  438. SCALER_DISPCTRLX_ENABLE) {
  439. HVS_WRITE(SCALER_DISPCTRLX(chan),
  440. SCALER_DISPCTRLX_RESET);
  441. /* While the docs say that reset is self-clearing, it
  442. * seems it doesn't actually.
  443. */
  444. HVS_WRITE(SCALER_DISPCTRLX(chan), 0);
  445. }
  446. /* Once we leave, the scaler should be disabled and its fifo empty. */
  447. WARN_ON_ONCE(HVS_READ(SCALER_DISPCTRLX(chan)) & SCALER_DISPCTRLX_RESET);
  448. WARN_ON_ONCE(VC4_GET_FIELD(HVS_READ(SCALER_DISPSTATX(chan)),
  449. SCALER_DISPSTATX_MODE) !=
  450. SCALER_DISPSTATX_MODE_DISABLED);
  451. WARN_ON_ONCE((HVS_READ(SCALER_DISPSTATX(chan)) &
  452. (SCALER_DISPSTATX_FULL | SCALER_DISPSTATX_EMPTY)) !=
  453. SCALER_DISPSTATX_EMPTY);
  454. /*
  455. * Make sure we issue a vblank event after disabling the CRTC if
  456. * someone was waiting it.
  457. */
  458. if (crtc->state->event) {
  459. unsigned long flags;
  460. spin_lock_irqsave(&dev->event_lock, flags);
  461. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  462. crtc->state->event = NULL;
  463. spin_unlock_irqrestore(&dev->event_lock, flags);
  464. }
  465. }
  466. void vc4_crtc_txp_armed(struct drm_crtc_state *state)
  467. {
  468. struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(state);
  469. vc4_state->txp_armed = true;
  470. }
  471. static void vc4_crtc_update_dlist(struct drm_crtc *crtc)
  472. {
  473. struct drm_device *dev = crtc->dev;
  474. struct vc4_dev *vc4 = to_vc4_dev(dev);
  475. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  476. struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc->state);
  477. if (crtc->state->event) {
  478. unsigned long flags;
  479. crtc->state->event->pipe = drm_crtc_index(crtc);
  480. WARN_ON(drm_crtc_vblank_get(crtc) != 0);
  481. spin_lock_irqsave(&dev->event_lock, flags);
  482. if (!vc4_state->feed_txp || vc4_state->txp_armed) {
  483. vc4_crtc->event = crtc->state->event;
  484. crtc->state->event = NULL;
  485. }
  486. HVS_WRITE(SCALER_DISPLISTX(vc4_crtc->channel),
  487. vc4_state->mm.start);
  488. spin_unlock_irqrestore(&dev->event_lock, flags);
  489. } else {
  490. HVS_WRITE(SCALER_DISPLISTX(vc4_crtc->channel),
  491. vc4_state->mm.start);
  492. }
  493. }
  494. static void vc4_crtc_atomic_enable(struct drm_crtc *crtc,
  495. struct drm_crtc_state *old_state)
  496. {
  497. struct drm_device *dev = crtc->dev;
  498. struct vc4_dev *vc4 = to_vc4_dev(dev);
  499. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  500. struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc->state);
  501. struct drm_display_mode *mode = &crtc->state->adjusted_mode;
  502. require_hvs_enabled(dev);
  503. /* Enable vblank irq handling before crtc is started otherwise
  504. * drm_crtc_get_vblank() fails in vc4_crtc_update_dlist().
  505. */
  506. drm_crtc_vblank_on(crtc);
  507. vc4_crtc_update_dlist(crtc);
  508. /* Turn on the scaler, which will wait for vstart to start
  509. * compositing.
  510. * When feeding the transposer, we should operate in oneshot
  511. * mode.
  512. */
  513. HVS_WRITE(SCALER_DISPCTRLX(vc4_crtc->channel),
  514. VC4_SET_FIELD(mode->hdisplay, SCALER_DISPCTRLX_WIDTH) |
  515. VC4_SET_FIELD(mode->vdisplay, SCALER_DISPCTRLX_HEIGHT) |
  516. SCALER_DISPCTRLX_ENABLE |
  517. (vc4_state->feed_txp ? SCALER_DISPCTRLX_ONESHOT : 0));
  518. /* When feeding the transposer block the pixelvalve is unneeded and
  519. * should not be enabled.
  520. */
  521. if (!vc4_state->feed_txp)
  522. CRTC_WRITE(PV_V_CONTROL,
  523. CRTC_READ(PV_V_CONTROL) | PV_VCONTROL_VIDEN);
  524. }
  525. static enum drm_mode_status vc4_crtc_mode_valid(struct drm_crtc *crtc,
  526. const struct drm_display_mode *mode)
  527. {
  528. /* Do not allow doublescan modes from user space */
  529. if (mode->flags & DRM_MODE_FLAG_DBLSCAN) {
  530. DRM_DEBUG_KMS("[CRTC:%d] Doublescan mode rejected.\n",
  531. crtc->base.id);
  532. return MODE_NO_DBLESCAN;
  533. }
  534. return MODE_OK;
  535. }
  536. static int vc4_crtc_atomic_check(struct drm_crtc *crtc,
  537. struct drm_crtc_state *state)
  538. {
  539. struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(state);
  540. struct drm_device *dev = crtc->dev;
  541. struct vc4_dev *vc4 = to_vc4_dev(dev);
  542. struct drm_plane *plane;
  543. unsigned long flags;
  544. const struct drm_plane_state *plane_state;
  545. struct drm_connector *conn;
  546. struct drm_connector_state *conn_state;
  547. u32 dlist_count = 0;
  548. int ret, i;
  549. /* The pixelvalve can only feed one encoder (and encoders are
  550. * 1:1 with connectors.)
  551. */
  552. if (hweight32(state->connector_mask) > 1)
  553. return -EINVAL;
  554. drm_atomic_crtc_state_for_each_plane_state(plane, plane_state, state)
  555. dlist_count += vc4_plane_dlist_size(plane_state);
  556. dlist_count++; /* Account for SCALER_CTL0_END. */
  557. spin_lock_irqsave(&vc4->hvs->mm_lock, flags);
  558. ret = drm_mm_insert_node(&vc4->hvs->dlist_mm, &vc4_state->mm,
  559. dlist_count);
  560. spin_unlock_irqrestore(&vc4->hvs->mm_lock, flags);
  561. if (ret)
  562. return ret;
  563. for_each_new_connector_in_state(state->state, conn, conn_state, i) {
  564. if (conn_state->crtc != crtc)
  565. continue;
  566. /* The writeback connector is implemented using the transposer
  567. * block which is directly taking its data from the HVS FIFO.
  568. */
  569. if (conn->connector_type == DRM_MODE_CONNECTOR_WRITEBACK) {
  570. state->no_vblank = true;
  571. vc4_state->feed_txp = true;
  572. } else {
  573. state->no_vblank = false;
  574. vc4_state->feed_txp = false;
  575. }
  576. break;
  577. }
  578. return 0;
  579. }
  580. static void vc4_crtc_atomic_flush(struct drm_crtc *crtc,
  581. struct drm_crtc_state *old_state)
  582. {
  583. struct drm_device *dev = crtc->dev;
  584. struct vc4_dev *vc4 = to_vc4_dev(dev);
  585. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  586. struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc->state);
  587. struct drm_plane *plane;
  588. struct vc4_plane_state *vc4_plane_state;
  589. bool debug_dump_regs = false;
  590. bool enable_bg_fill = false;
  591. u32 __iomem *dlist_start = vc4->hvs->dlist + vc4_state->mm.start;
  592. u32 __iomem *dlist_next = dlist_start;
  593. if (debug_dump_regs) {
  594. DRM_INFO("CRTC %d HVS before:\n", drm_crtc_index(crtc));
  595. vc4_hvs_dump_state(dev);
  596. }
  597. /* Copy all the active planes' dlist contents to the hardware dlist. */
  598. drm_atomic_crtc_for_each_plane(plane, crtc) {
  599. /* Is this the first active plane? */
  600. if (dlist_next == dlist_start) {
  601. /* We need to enable background fill when a plane
  602. * could be alpha blending from the background, i.e.
  603. * where no other plane is underneath. It suffices to
  604. * consider the first active plane here since we set
  605. * needs_bg_fill such that either the first plane
  606. * already needs it or all planes on top blend from
  607. * the first or a lower plane.
  608. */
  609. vc4_plane_state = to_vc4_plane_state(plane->state);
  610. enable_bg_fill = vc4_plane_state->needs_bg_fill;
  611. }
  612. dlist_next += vc4_plane_write_dlist(plane, dlist_next);
  613. }
  614. writel(SCALER_CTL0_END, dlist_next);
  615. dlist_next++;
  616. WARN_ON_ONCE(dlist_next - dlist_start != vc4_state->mm.size);
  617. if (enable_bg_fill)
  618. /* This sets a black background color fill, as is the case
  619. * with other DRM drivers.
  620. */
  621. HVS_WRITE(SCALER_DISPBKGNDX(vc4_crtc->channel),
  622. HVS_READ(SCALER_DISPBKGNDX(vc4_crtc->channel)) |
  623. SCALER_DISPBKGND_FILL);
  624. /* Only update DISPLIST if the CRTC was already running and is not
  625. * being disabled.
  626. * vc4_crtc_enable() takes care of updating the dlist just after
  627. * re-enabling VBLANK interrupts and before enabling the engine.
  628. * If the CRTC is being disabled, there's no point in updating this
  629. * information.
  630. */
  631. if (crtc->state->active && old_state->active)
  632. vc4_crtc_update_dlist(crtc);
  633. if (crtc->state->color_mgmt_changed) {
  634. u32 dispbkgndx = HVS_READ(SCALER_DISPBKGNDX(vc4_crtc->channel));
  635. if (crtc->state->gamma_lut) {
  636. vc4_crtc_update_gamma_lut(crtc);
  637. dispbkgndx |= SCALER_DISPBKGND_GAMMA;
  638. } else {
  639. /* Unsetting DISPBKGND_GAMMA skips the gamma lut step
  640. * in hardware, which is the same as a linear lut that
  641. * DRM expects us to use in absence of a user lut.
  642. */
  643. dispbkgndx &= ~SCALER_DISPBKGND_GAMMA;
  644. }
  645. HVS_WRITE(SCALER_DISPBKGNDX(vc4_crtc->channel), dispbkgndx);
  646. }
  647. if (debug_dump_regs) {
  648. DRM_INFO("CRTC %d HVS after:\n", drm_crtc_index(crtc));
  649. vc4_hvs_dump_state(dev);
  650. }
  651. }
  652. static int vc4_enable_vblank(struct drm_crtc *crtc)
  653. {
  654. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  655. CRTC_WRITE(PV_INTEN, PV_INT_VFP_START);
  656. return 0;
  657. }
  658. static void vc4_disable_vblank(struct drm_crtc *crtc)
  659. {
  660. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  661. CRTC_WRITE(PV_INTEN, 0);
  662. }
  663. static void vc4_crtc_handle_page_flip(struct vc4_crtc *vc4_crtc)
  664. {
  665. struct drm_crtc *crtc = &vc4_crtc->base;
  666. struct drm_device *dev = crtc->dev;
  667. struct vc4_dev *vc4 = to_vc4_dev(dev);
  668. struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc->state);
  669. u32 chan = vc4_crtc->channel;
  670. unsigned long flags;
  671. spin_lock_irqsave(&dev->event_lock, flags);
  672. if (vc4_crtc->event &&
  673. (vc4_state->mm.start == HVS_READ(SCALER_DISPLACTX(chan)) ||
  674. vc4_state->feed_txp)) {
  675. drm_crtc_send_vblank_event(crtc, vc4_crtc->event);
  676. vc4_crtc->event = NULL;
  677. drm_crtc_vblank_put(crtc);
  678. }
  679. spin_unlock_irqrestore(&dev->event_lock, flags);
  680. }
  681. void vc4_crtc_handle_vblank(struct vc4_crtc *crtc)
  682. {
  683. crtc->t_vblank = ktime_get();
  684. drm_crtc_handle_vblank(&crtc->base);
  685. vc4_crtc_handle_page_flip(crtc);
  686. }
  687. static irqreturn_t vc4_crtc_irq_handler(int irq, void *data)
  688. {
  689. struct vc4_crtc *vc4_crtc = data;
  690. u32 stat = CRTC_READ(PV_INTSTAT);
  691. irqreturn_t ret = IRQ_NONE;
  692. if (stat & PV_INT_VFP_START) {
  693. CRTC_WRITE(PV_INTSTAT, PV_INT_VFP_START);
  694. vc4_crtc_handle_vblank(vc4_crtc);
  695. ret = IRQ_HANDLED;
  696. }
  697. return ret;
  698. }
  699. struct vc4_async_flip_state {
  700. struct drm_crtc *crtc;
  701. struct drm_framebuffer *fb;
  702. struct drm_framebuffer *old_fb;
  703. struct drm_pending_vblank_event *event;
  704. struct vc4_seqno_cb cb;
  705. };
  706. /* Called when the V3D execution for the BO being flipped to is done, so that
  707. * we can actually update the plane's address to point to it.
  708. */
  709. static void
  710. vc4_async_page_flip_complete(struct vc4_seqno_cb *cb)
  711. {
  712. struct vc4_async_flip_state *flip_state =
  713. container_of(cb, struct vc4_async_flip_state, cb);
  714. struct drm_crtc *crtc = flip_state->crtc;
  715. struct drm_device *dev = crtc->dev;
  716. struct vc4_dev *vc4 = to_vc4_dev(dev);
  717. struct drm_plane *plane = crtc->primary;
  718. vc4_plane_async_set_fb(plane, flip_state->fb);
  719. if (flip_state->event) {
  720. unsigned long flags;
  721. spin_lock_irqsave(&dev->event_lock, flags);
  722. drm_crtc_send_vblank_event(crtc, flip_state->event);
  723. spin_unlock_irqrestore(&dev->event_lock, flags);
  724. }
  725. drm_crtc_vblank_put(crtc);
  726. drm_framebuffer_put(flip_state->fb);
  727. /* Decrement the BO usecnt in order to keep the inc/dec calls balanced
  728. * when the planes are updated through the async update path.
  729. * FIXME: we should move to generic async-page-flip when it's
  730. * available, so that we can get rid of this hand-made cleanup_fb()
  731. * logic.
  732. */
  733. if (flip_state->old_fb) {
  734. struct drm_gem_cma_object *cma_bo;
  735. struct vc4_bo *bo;
  736. cma_bo = drm_fb_cma_get_gem_obj(flip_state->old_fb, 0);
  737. bo = to_vc4_bo(&cma_bo->base);
  738. vc4_bo_dec_usecnt(bo);
  739. drm_framebuffer_put(flip_state->old_fb);
  740. }
  741. kfree(flip_state);
  742. up(&vc4->async_modeset);
  743. }
  744. /* Implements async (non-vblank-synced) page flips.
  745. *
  746. * The page flip ioctl needs to return immediately, so we grab the
  747. * modeset semaphore on the pipe, and queue the address update for
  748. * when V3D is done with the BO being flipped to.
  749. */
  750. static int vc4_async_page_flip(struct drm_crtc *crtc,
  751. struct drm_framebuffer *fb,
  752. struct drm_pending_vblank_event *event,
  753. uint32_t flags)
  754. {
  755. struct drm_device *dev = crtc->dev;
  756. struct vc4_dev *vc4 = to_vc4_dev(dev);
  757. struct drm_plane *plane = crtc->primary;
  758. int ret = 0;
  759. struct vc4_async_flip_state *flip_state;
  760. struct drm_gem_cma_object *cma_bo = drm_fb_cma_get_gem_obj(fb, 0);
  761. struct vc4_bo *bo = to_vc4_bo(&cma_bo->base);
  762. /* Increment the BO usecnt here, so that we never end up with an
  763. * unbalanced number of vc4_bo_{dec,inc}_usecnt() calls when the
  764. * plane is later updated through the non-async path.
  765. * FIXME: we should move to generic async-page-flip when it's
  766. * available, so that we can get rid of this hand-made prepare_fb()
  767. * logic.
  768. */
  769. ret = vc4_bo_inc_usecnt(bo);
  770. if (ret)
  771. return ret;
  772. flip_state = kzalloc(sizeof(*flip_state), GFP_KERNEL);
  773. if (!flip_state) {
  774. vc4_bo_dec_usecnt(bo);
  775. return -ENOMEM;
  776. }
  777. drm_framebuffer_get(fb);
  778. flip_state->fb = fb;
  779. flip_state->crtc = crtc;
  780. flip_state->event = event;
  781. /* Make sure all other async modesetes have landed. */
  782. ret = down_interruptible(&vc4->async_modeset);
  783. if (ret) {
  784. drm_framebuffer_put(fb);
  785. vc4_bo_dec_usecnt(bo);
  786. kfree(flip_state);
  787. return ret;
  788. }
  789. /* Save the current FB before it's replaced by the new one in
  790. * drm_atomic_set_fb_for_plane(). We'll need the old FB in
  791. * vc4_async_page_flip_complete() to decrement the BO usecnt and keep
  792. * it consistent.
  793. * FIXME: we should move to generic async-page-flip when it's
  794. * available, so that we can get rid of this hand-made cleanup_fb()
  795. * logic.
  796. */
  797. flip_state->old_fb = plane->state->fb;
  798. if (flip_state->old_fb)
  799. drm_framebuffer_get(flip_state->old_fb);
  800. WARN_ON(drm_crtc_vblank_get(crtc) != 0);
  801. /* Immediately update the plane's legacy fb pointer, so that later
  802. * modeset prep sees the state that will be present when the semaphore
  803. * is released.
  804. */
  805. drm_atomic_set_fb_for_plane(plane->state, fb);
  806. vc4_queue_seqno_cb(dev, &flip_state->cb, bo->seqno,
  807. vc4_async_page_flip_complete);
  808. /* Driver takes ownership of state on successful async commit. */
  809. return 0;
  810. }
  811. static int vc4_page_flip(struct drm_crtc *crtc,
  812. struct drm_framebuffer *fb,
  813. struct drm_pending_vblank_event *event,
  814. uint32_t flags,
  815. struct drm_modeset_acquire_ctx *ctx)
  816. {
  817. if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
  818. return vc4_async_page_flip(crtc, fb, event, flags);
  819. else
  820. return drm_atomic_helper_page_flip(crtc, fb, event, flags, ctx);
  821. }
  822. static struct drm_crtc_state *vc4_crtc_duplicate_state(struct drm_crtc *crtc)
  823. {
  824. struct vc4_crtc_state *vc4_state, *old_vc4_state;
  825. vc4_state = kzalloc(sizeof(*vc4_state), GFP_KERNEL);
  826. if (!vc4_state)
  827. return NULL;
  828. old_vc4_state = to_vc4_crtc_state(crtc->state);
  829. vc4_state->feed_txp = old_vc4_state->feed_txp;
  830. __drm_atomic_helper_crtc_duplicate_state(crtc, &vc4_state->base);
  831. return &vc4_state->base;
  832. }
  833. static void vc4_crtc_destroy_state(struct drm_crtc *crtc,
  834. struct drm_crtc_state *state)
  835. {
  836. struct vc4_dev *vc4 = to_vc4_dev(crtc->dev);
  837. struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(state);
  838. if (vc4_state->mm.allocated) {
  839. unsigned long flags;
  840. spin_lock_irqsave(&vc4->hvs->mm_lock, flags);
  841. drm_mm_remove_node(&vc4_state->mm);
  842. spin_unlock_irqrestore(&vc4->hvs->mm_lock, flags);
  843. }
  844. drm_atomic_helper_crtc_destroy_state(crtc, state);
  845. }
  846. static void
  847. vc4_crtc_reset(struct drm_crtc *crtc)
  848. {
  849. if (crtc->state)
  850. vc4_crtc_destroy_state(crtc, crtc->state);
  851. crtc->state = kzalloc(sizeof(struct vc4_crtc_state), GFP_KERNEL);
  852. if (crtc->state)
  853. crtc->state->crtc = crtc;
  854. }
  855. static const struct drm_crtc_funcs vc4_crtc_funcs = {
  856. .set_config = drm_atomic_helper_set_config,
  857. .destroy = vc4_crtc_destroy,
  858. .page_flip = vc4_page_flip,
  859. .set_property = NULL,
  860. .cursor_set = NULL, /* handled by drm_mode_cursor_universal */
  861. .cursor_move = NULL, /* handled by drm_mode_cursor_universal */
  862. .reset = vc4_crtc_reset,
  863. .atomic_duplicate_state = vc4_crtc_duplicate_state,
  864. .atomic_destroy_state = vc4_crtc_destroy_state,
  865. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  866. .enable_vblank = vc4_enable_vblank,
  867. .disable_vblank = vc4_disable_vblank,
  868. };
  869. static const struct drm_crtc_helper_funcs vc4_crtc_helper_funcs = {
  870. .mode_set_nofb = vc4_crtc_mode_set_nofb,
  871. .mode_valid = vc4_crtc_mode_valid,
  872. .atomic_check = vc4_crtc_atomic_check,
  873. .atomic_flush = vc4_crtc_atomic_flush,
  874. .atomic_enable = vc4_crtc_atomic_enable,
  875. .atomic_disable = vc4_crtc_atomic_disable,
  876. };
  877. static const struct vc4_crtc_data pv0_data = {
  878. .hvs_channel = 0,
  879. .encoder_types = {
  880. [PV_CONTROL_CLK_SELECT_DSI] = VC4_ENCODER_TYPE_DSI0,
  881. [PV_CONTROL_CLK_SELECT_DPI_SMI_HDMI] = VC4_ENCODER_TYPE_DPI,
  882. },
  883. };
  884. static const struct vc4_crtc_data pv1_data = {
  885. .hvs_channel = 2,
  886. .encoder_types = {
  887. [PV_CONTROL_CLK_SELECT_DSI] = VC4_ENCODER_TYPE_DSI1,
  888. [PV_CONTROL_CLK_SELECT_DPI_SMI_HDMI] = VC4_ENCODER_TYPE_SMI,
  889. },
  890. };
  891. static const struct vc4_crtc_data pv2_data = {
  892. .hvs_channel = 1,
  893. .encoder_types = {
  894. [PV_CONTROL_CLK_SELECT_DPI_SMI_HDMI] = VC4_ENCODER_TYPE_HDMI,
  895. [PV_CONTROL_CLK_SELECT_VEC] = VC4_ENCODER_TYPE_VEC,
  896. },
  897. };
  898. static const struct of_device_id vc4_crtc_dt_match[] = {
  899. { .compatible = "brcm,bcm2835-pixelvalve0", .data = &pv0_data },
  900. { .compatible = "brcm,bcm2835-pixelvalve1", .data = &pv1_data },
  901. { .compatible = "brcm,bcm2835-pixelvalve2", .data = &pv2_data },
  902. {}
  903. };
  904. static void vc4_set_crtc_possible_masks(struct drm_device *drm,
  905. struct drm_crtc *crtc)
  906. {
  907. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  908. const struct vc4_crtc_data *crtc_data = vc4_crtc->data;
  909. const enum vc4_encoder_type *encoder_types = crtc_data->encoder_types;
  910. struct drm_encoder *encoder;
  911. drm_for_each_encoder(encoder, drm) {
  912. struct vc4_encoder *vc4_encoder;
  913. int i;
  914. /* HVS FIFO2 can feed the TXP IP. */
  915. if (crtc_data->hvs_channel == 2 &&
  916. encoder->encoder_type == DRM_MODE_ENCODER_VIRTUAL) {
  917. encoder->possible_crtcs |= drm_crtc_mask(crtc);
  918. continue;
  919. }
  920. vc4_encoder = to_vc4_encoder(encoder);
  921. for (i = 0; i < ARRAY_SIZE(crtc_data->encoder_types); i++) {
  922. if (vc4_encoder->type == encoder_types[i]) {
  923. vc4_encoder->clock_select = i;
  924. encoder->possible_crtcs |= drm_crtc_mask(crtc);
  925. break;
  926. }
  927. }
  928. }
  929. }
  930. static void
  931. vc4_crtc_get_cob_allocation(struct vc4_crtc *vc4_crtc)
  932. {
  933. struct drm_device *drm = vc4_crtc->base.dev;
  934. struct vc4_dev *vc4 = to_vc4_dev(drm);
  935. u32 dispbase = HVS_READ(SCALER_DISPBASEX(vc4_crtc->channel));
  936. /* Top/base are supposed to be 4-pixel aligned, but the
  937. * Raspberry Pi firmware fills the low bits (which are
  938. * presumably ignored).
  939. */
  940. u32 top = VC4_GET_FIELD(dispbase, SCALER_DISPBASEX_TOP) & ~3;
  941. u32 base = VC4_GET_FIELD(dispbase, SCALER_DISPBASEX_BASE) & ~3;
  942. vc4_crtc->cob_size = top - base + 4;
  943. }
  944. static int vc4_crtc_bind(struct device *dev, struct device *master, void *data)
  945. {
  946. struct platform_device *pdev = to_platform_device(dev);
  947. struct drm_device *drm = dev_get_drvdata(master);
  948. struct vc4_crtc *vc4_crtc;
  949. struct drm_crtc *crtc;
  950. struct drm_plane *primary_plane, *cursor_plane, *destroy_plane, *temp;
  951. const struct of_device_id *match;
  952. int ret, i;
  953. vc4_crtc = devm_kzalloc(dev, sizeof(*vc4_crtc), GFP_KERNEL);
  954. if (!vc4_crtc)
  955. return -ENOMEM;
  956. crtc = &vc4_crtc->base;
  957. match = of_match_device(vc4_crtc_dt_match, dev);
  958. if (!match)
  959. return -ENODEV;
  960. vc4_crtc->data = match->data;
  961. vc4_crtc->regs = vc4_ioremap_regs(pdev, 0);
  962. if (IS_ERR(vc4_crtc->regs))
  963. return PTR_ERR(vc4_crtc->regs);
  964. /* For now, we create just the primary and the legacy cursor
  965. * planes. We should be able to stack more planes on easily,
  966. * but to do that we would need to compute the bandwidth
  967. * requirement of the plane configuration, and reject ones
  968. * that will take too much.
  969. */
  970. primary_plane = vc4_plane_init(drm, DRM_PLANE_TYPE_PRIMARY);
  971. if (IS_ERR(primary_plane)) {
  972. dev_err(dev, "failed to construct primary plane\n");
  973. ret = PTR_ERR(primary_plane);
  974. goto err;
  975. }
  976. drm_crtc_init_with_planes(drm, crtc, primary_plane, NULL,
  977. &vc4_crtc_funcs, NULL);
  978. drm_crtc_helper_add(crtc, &vc4_crtc_helper_funcs);
  979. vc4_crtc->channel = vc4_crtc->data->hvs_channel;
  980. drm_mode_crtc_set_gamma_size(crtc, ARRAY_SIZE(vc4_crtc->lut_r));
  981. drm_crtc_enable_color_mgmt(crtc, 0, false, crtc->gamma_size);
  982. /* We support CTM, but only for one CRTC at a time. It's therefore
  983. * implemented as private driver state in vc4_kms, not here.
  984. */
  985. drm_crtc_enable_color_mgmt(crtc, 0, true, crtc->gamma_size);
  986. /* Set up some arbitrary number of planes. We're not limited
  987. * by a set number of physical registers, just the space in
  988. * the HVS (16k) and how small an plane can be (28 bytes).
  989. * However, each plane we set up takes up some memory, and
  990. * increases the cost of looping over planes, which atomic
  991. * modesetting does quite a bit. As a result, we pick a
  992. * modest number of planes to expose, that should hopefully
  993. * still cover any sane usecase.
  994. */
  995. for (i = 0; i < 8; i++) {
  996. struct drm_plane *plane =
  997. vc4_plane_init(drm, DRM_PLANE_TYPE_OVERLAY);
  998. if (IS_ERR(plane))
  999. continue;
  1000. plane->possible_crtcs = drm_crtc_mask(crtc);
  1001. }
  1002. /* Set up the legacy cursor after overlay initialization,
  1003. * since we overlay planes on the CRTC in the order they were
  1004. * initialized.
  1005. */
  1006. cursor_plane = vc4_plane_init(drm, DRM_PLANE_TYPE_CURSOR);
  1007. if (!IS_ERR(cursor_plane)) {
  1008. cursor_plane->possible_crtcs = drm_crtc_mask(crtc);
  1009. crtc->cursor = cursor_plane;
  1010. }
  1011. vc4_crtc_get_cob_allocation(vc4_crtc);
  1012. CRTC_WRITE(PV_INTEN, 0);
  1013. CRTC_WRITE(PV_INTSTAT, PV_INT_VFP_START);
  1014. ret = devm_request_irq(dev, platform_get_irq(pdev, 0),
  1015. vc4_crtc_irq_handler, 0, "vc4 crtc", vc4_crtc);
  1016. if (ret)
  1017. goto err_destroy_planes;
  1018. vc4_set_crtc_possible_masks(drm, crtc);
  1019. for (i = 0; i < crtc->gamma_size; i++) {
  1020. vc4_crtc->lut_r[i] = i;
  1021. vc4_crtc->lut_g[i] = i;
  1022. vc4_crtc->lut_b[i] = i;
  1023. }
  1024. platform_set_drvdata(pdev, vc4_crtc);
  1025. return 0;
  1026. err_destroy_planes:
  1027. list_for_each_entry_safe(destroy_plane, temp,
  1028. &drm->mode_config.plane_list, head) {
  1029. if (destroy_plane->possible_crtcs == drm_crtc_mask(crtc))
  1030. destroy_plane->funcs->destroy(destroy_plane);
  1031. }
  1032. err:
  1033. return ret;
  1034. }
  1035. static void vc4_crtc_unbind(struct device *dev, struct device *master,
  1036. void *data)
  1037. {
  1038. struct platform_device *pdev = to_platform_device(dev);
  1039. struct vc4_crtc *vc4_crtc = dev_get_drvdata(dev);
  1040. vc4_crtc_destroy(&vc4_crtc->base);
  1041. CRTC_WRITE(PV_INTEN, 0);
  1042. platform_set_drvdata(pdev, NULL);
  1043. }
  1044. static const struct component_ops vc4_crtc_ops = {
  1045. .bind = vc4_crtc_bind,
  1046. .unbind = vc4_crtc_unbind,
  1047. };
  1048. static int vc4_crtc_dev_probe(struct platform_device *pdev)
  1049. {
  1050. return component_add(&pdev->dev, &vc4_crtc_ops);
  1051. }
  1052. static int vc4_crtc_dev_remove(struct platform_device *pdev)
  1053. {
  1054. component_del(&pdev->dev, &vc4_crtc_ops);
  1055. return 0;
  1056. }
  1057. struct platform_driver vc4_crtc_driver = {
  1058. .probe = vc4_crtc_dev_probe,
  1059. .remove = vc4_crtc_dev_remove,
  1060. .driver = {
  1061. .name = "vc4_crtc",
  1062. .of_match_table = vc4_crtc_dt_match,
  1063. },
  1064. };