vc4_crtc.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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. * output'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, this file also manages
  18. * setup of the VC4 HVS's display elements on the CRTC.
  19. *
  20. * The 2835 has 3 different pixel valves. pv0 in the audio power
  21. * domain feeds DSI0 or DPI, while pv1 feeds DS1 or SMI. pv2 in the
  22. * image domain can feed either HDMI or the SDTV controller. The
  23. * pixel valve chooses from the CPRMAN clocks (HSM for HDMI, VEC for
  24. * SDTV, etc.) according to which output type is chosen in the mux.
  25. *
  26. * For power management, the pixel valve's registers are all clocked
  27. * by the AXI clock, while the timings and FIFOs make use of the
  28. * output-specific clock. Since the encoders also directly consume
  29. * the CPRMAN clocks, and know what timings they need, they are the
  30. * ones that set the clock.
  31. */
  32. #include "drm_atomic.h"
  33. #include "drm_atomic_helper.h"
  34. #include "drm_crtc_helper.h"
  35. #include "linux/clk.h"
  36. #include "drm_fb_cma_helper.h"
  37. #include "linux/component.h"
  38. #include "linux/of_device.h"
  39. #include "vc4_drv.h"
  40. #include "vc4_regs.h"
  41. struct vc4_crtc {
  42. struct drm_crtc base;
  43. const struct vc4_crtc_data *data;
  44. void __iomem *regs;
  45. /* Timestamp at start of vblank irq - unaffected by lock delays. */
  46. ktime_t t_vblank;
  47. /* Which HVS channel we're using for our CRTC. */
  48. int channel;
  49. u8 lut_r[256];
  50. u8 lut_g[256];
  51. u8 lut_b[256];
  52. /* Size in pixels of the COB memory allocated to this CRTC. */
  53. u32 cob_size;
  54. struct drm_pending_vblank_event *event;
  55. };
  56. struct vc4_crtc_state {
  57. struct drm_crtc_state base;
  58. /* Dlist area for this CRTC configuration. */
  59. struct drm_mm_node mm;
  60. };
  61. static inline struct vc4_crtc *
  62. to_vc4_crtc(struct drm_crtc *crtc)
  63. {
  64. return (struct vc4_crtc *)crtc;
  65. }
  66. static inline struct vc4_crtc_state *
  67. to_vc4_crtc_state(struct drm_crtc_state *crtc_state)
  68. {
  69. return (struct vc4_crtc_state *)crtc_state;
  70. }
  71. struct vc4_crtc_data {
  72. /* Which channel of the HVS this pixelvalve sources from. */
  73. int hvs_channel;
  74. enum vc4_encoder_type encoder0_type;
  75. enum vc4_encoder_type encoder1_type;
  76. };
  77. #define CRTC_WRITE(offset, val) writel(val, vc4_crtc->regs + (offset))
  78. #define CRTC_READ(offset) readl(vc4_crtc->regs + (offset))
  79. #define CRTC_REG(reg) { reg, #reg }
  80. static const struct {
  81. u32 reg;
  82. const char *name;
  83. } crtc_regs[] = {
  84. CRTC_REG(PV_CONTROL),
  85. CRTC_REG(PV_V_CONTROL),
  86. CRTC_REG(PV_VSYNCD_EVEN),
  87. CRTC_REG(PV_HORZA),
  88. CRTC_REG(PV_HORZB),
  89. CRTC_REG(PV_VERTA),
  90. CRTC_REG(PV_VERTB),
  91. CRTC_REG(PV_VERTA_EVEN),
  92. CRTC_REG(PV_VERTB_EVEN),
  93. CRTC_REG(PV_INTEN),
  94. CRTC_REG(PV_INTSTAT),
  95. CRTC_REG(PV_STAT),
  96. CRTC_REG(PV_HACT_ACT),
  97. };
  98. static void vc4_crtc_dump_regs(struct vc4_crtc *vc4_crtc)
  99. {
  100. int i;
  101. for (i = 0; i < ARRAY_SIZE(crtc_regs); i++) {
  102. DRM_INFO("0x%04x (%s): 0x%08x\n",
  103. crtc_regs[i].reg, crtc_regs[i].name,
  104. CRTC_READ(crtc_regs[i].reg));
  105. }
  106. }
  107. #ifdef CONFIG_DEBUG_FS
  108. int vc4_crtc_debugfs_regs(struct seq_file *m, void *unused)
  109. {
  110. struct drm_info_node *node = (struct drm_info_node *)m->private;
  111. struct drm_device *dev = node->minor->dev;
  112. int crtc_index = (uintptr_t)node->info_ent->data;
  113. struct drm_crtc *crtc;
  114. struct vc4_crtc *vc4_crtc;
  115. int i;
  116. i = 0;
  117. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  118. if (i == crtc_index)
  119. break;
  120. i++;
  121. }
  122. if (!crtc)
  123. return 0;
  124. vc4_crtc = to_vc4_crtc(crtc);
  125. for (i = 0; i < ARRAY_SIZE(crtc_regs); i++) {
  126. seq_printf(m, "%s (0x%04x): 0x%08x\n",
  127. crtc_regs[i].name, crtc_regs[i].reg,
  128. CRTC_READ(crtc_regs[i].reg));
  129. }
  130. return 0;
  131. }
  132. #endif
  133. int vc4_crtc_get_scanoutpos(struct drm_device *dev, unsigned int crtc_id,
  134. unsigned int flags, int *vpos, int *hpos,
  135. ktime_t *stime, ktime_t *etime,
  136. const struct drm_display_mode *mode)
  137. {
  138. struct vc4_dev *vc4 = to_vc4_dev(dev);
  139. struct vc4_crtc *vc4_crtc = vc4->crtc[crtc_id];
  140. u32 val;
  141. int fifo_lines;
  142. int vblank_lines;
  143. int ret = 0;
  144. /*
  145. * XXX Doesn't work well in interlaced mode yet, partially due
  146. * to problems in vc4 kms or drm core interlaced mode handling,
  147. * so disable for now in interlaced mode.
  148. */
  149. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  150. return ret;
  151. /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
  152. /* Get optional system timestamp before query. */
  153. if (stime)
  154. *stime = ktime_get();
  155. /*
  156. * Read vertical scanline which is currently composed for our
  157. * pixelvalve by the HVS, and also the scaler status.
  158. */
  159. val = HVS_READ(SCALER_DISPSTATX(vc4_crtc->channel));
  160. /* Get optional system timestamp after query. */
  161. if (etime)
  162. *etime = ktime_get();
  163. /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
  164. /* Vertical position of hvs composed scanline. */
  165. *vpos = VC4_GET_FIELD(val, SCALER_DISPSTATX_LINE);
  166. /* No hpos info available. */
  167. if (hpos)
  168. *hpos = 0;
  169. /* This is the offset we need for translating hvs -> pv scanout pos. */
  170. fifo_lines = vc4_crtc->cob_size / mode->crtc_hdisplay;
  171. if (fifo_lines > 0)
  172. ret |= DRM_SCANOUTPOS_VALID;
  173. /* HVS more than fifo_lines into frame for compositing? */
  174. if (*vpos > fifo_lines) {
  175. /*
  176. * We are in active scanout and can get some meaningful results
  177. * from HVS. The actual PV scanout can not trail behind more
  178. * than fifo_lines as that is the fifo's capacity. Assume that
  179. * in active scanout the HVS and PV work in lockstep wrt. HVS
  180. * refilling the fifo and PV consuming from the fifo, ie.
  181. * whenever the PV consumes and frees up a scanline in the
  182. * fifo, the HVS will immediately refill it, therefore
  183. * incrementing vpos. Therefore we choose HVS read position -
  184. * fifo size in scanlines as a estimate of the real scanout
  185. * position of the PV.
  186. */
  187. *vpos -= fifo_lines + 1;
  188. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  189. *vpos /= 2;
  190. ret |= DRM_SCANOUTPOS_ACCURATE;
  191. return ret;
  192. }
  193. /*
  194. * Less: This happens when we are in vblank and the HVS, after getting
  195. * the VSTART restart signal from the PV, just started refilling its
  196. * fifo with new lines from the top-most lines of the new framebuffers.
  197. * The PV does not scan out in vblank, so does not remove lines from
  198. * the fifo, so the fifo will be full quickly and the HVS has to pause.
  199. * We can't get meaningful readings wrt. scanline position of the PV
  200. * and need to make things up in a approximative but consistent way.
  201. */
  202. ret |= DRM_SCANOUTPOS_IN_VBLANK;
  203. vblank_lines = mode->crtc_vtotal - mode->crtc_vdisplay;
  204. if (flags & DRM_CALLED_FROM_VBLIRQ) {
  205. /*
  206. * Assume the irq handler got called close to first
  207. * line of vblank, so PV has about a full vblank
  208. * scanlines to go, and as a base timestamp use the
  209. * one taken at entry into vblank irq handler, so it
  210. * is not affected by random delays due to lock
  211. * contention on event_lock or vblank_time lock in
  212. * the core.
  213. */
  214. *vpos = -vblank_lines;
  215. if (stime)
  216. *stime = vc4_crtc->t_vblank;
  217. if (etime)
  218. *etime = vc4_crtc->t_vblank;
  219. /*
  220. * If the HVS fifo is not yet full then we know for certain
  221. * we are at the very beginning of vblank, as the hvs just
  222. * started refilling, and the stime and etime timestamps
  223. * truly correspond to start of vblank.
  224. */
  225. if ((val & SCALER_DISPSTATX_FULL) != SCALER_DISPSTATX_FULL)
  226. ret |= DRM_SCANOUTPOS_ACCURATE;
  227. } else {
  228. /*
  229. * No clue where we are inside vblank. Return a vpos of zero,
  230. * which will cause calling code to just return the etime
  231. * timestamp uncorrected. At least this is no worse than the
  232. * standard fallback.
  233. */
  234. *vpos = 0;
  235. }
  236. return ret;
  237. }
  238. int vc4_crtc_get_vblank_timestamp(struct drm_device *dev, unsigned int crtc_id,
  239. int *max_error, struct timeval *vblank_time,
  240. unsigned flags)
  241. {
  242. struct vc4_dev *vc4 = to_vc4_dev(dev);
  243. struct vc4_crtc *vc4_crtc = vc4->crtc[crtc_id];
  244. struct drm_crtc *crtc = &vc4_crtc->base;
  245. struct drm_crtc_state *state = crtc->state;
  246. /* Helper routine in DRM core does all the work: */
  247. return drm_calc_vbltimestamp_from_scanoutpos(dev, crtc_id, max_error,
  248. vblank_time, flags,
  249. &state->adjusted_mode);
  250. }
  251. static void vc4_crtc_destroy(struct drm_crtc *crtc)
  252. {
  253. drm_crtc_cleanup(crtc);
  254. }
  255. static void
  256. vc4_crtc_lut_load(struct drm_crtc *crtc)
  257. {
  258. struct drm_device *dev = crtc->dev;
  259. struct vc4_dev *vc4 = to_vc4_dev(dev);
  260. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  261. u32 i;
  262. /* The LUT memory is laid out with each HVS channel in order,
  263. * each of which takes 256 writes for R, 256 for G, then 256
  264. * for B.
  265. */
  266. HVS_WRITE(SCALER_GAMADDR,
  267. SCALER_GAMADDR_AUTOINC |
  268. (vc4_crtc->channel * 3 * crtc->gamma_size));
  269. for (i = 0; i < crtc->gamma_size; i++)
  270. HVS_WRITE(SCALER_GAMDATA, vc4_crtc->lut_r[i]);
  271. for (i = 0; i < crtc->gamma_size; i++)
  272. HVS_WRITE(SCALER_GAMDATA, vc4_crtc->lut_g[i]);
  273. for (i = 0; i < crtc->gamma_size; i++)
  274. HVS_WRITE(SCALER_GAMDATA, vc4_crtc->lut_b[i]);
  275. }
  276. static int
  277. vc4_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
  278. uint32_t size)
  279. {
  280. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  281. u32 i;
  282. for (i = 0; i < size; i++) {
  283. vc4_crtc->lut_r[i] = r[i] >> 8;
  284. vc4_crtc->lut_g[i] = g[i] >> 8;
  285. vc4_crtc->lut_b[i] = b[i] >> 8;
  286. }
  287. vc4_crtc_lut_load(crtc);
  288. return 0;
  289. }
  290. static u32 vc4_get_fifo_full_level(u32 format)
  291. {
  292. static const u32 fifo_len_bytes = 64;
  293. static const u32 hvs_latency_pix = 6;
  294. switch (format) {
  295. case PV_CONTROL_FORMAT_DSIV_16:
  296. case PV_CONTROL_FORMAT_DSIC_16:
  297. return fifo_len_bytes - 2 * hvs_latency_pix;
  298. case PV_CONTROL_FORMAT_DSIV_18:
  299. return fifo_len_bytes - 14;
  300. case PV_CONTROL_FORMAT_24:
  301. case PV_CONTROL_FORMAT_DSIV_24:
  302. default:
  303. return fifo_len_bytes - 3 * hvs_latency_pix;
  304. }
  305. }
  306. /*
  307. * Returns the clock select bit for the connector attached to the
  308. * CRTC.
  309. */
  310. static int vc4_get_clock_select(struct drm_crtc *crtc)
  311. {
  312. struct drm_connector *connector;
  313. drm_for_each_connector(connector, crtc->dev) {
  314. if (connector->state->crtc == crtc) {
  315. struct drm_encoder *encoder = connector->encoder;
  316. struct vc4_encoder *vc4_encoder =
  317. to_vc4_encoder(encoder);
  318. return vc4_encoder->clock_select;
  319. }
  320. }
  321. return -1;
  322. }
  323. static void vc4_crtc_mode_set_nofb(struct drm_crtc *crtc)
  324. {
  325. struct drm_device *dev = crtc->dev;
  326. struct vc4_dev *vc4 = to_vc4_dev(dev);
  327. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  328. struct drm_crtc_state *state = crtc->state;
  329. struct drm_display_mode *mode = &state->adjusted_mode;
  330. bool interlace = mode->flags & DRM_MODE_FLAG_INTERLACE;
  331. u32 vactive = (mode->vdisplay >> (interlace ? 1 : 0));
  332. u32 format = PV_CONTROL_FORMAT_24;
  333. bool debug_dump_regs = false;
  334. int clock_select = vc4_get_clock_select(crtc);
  335. if (debug_dump_regs) {
  336. DRM_INFO("CRTC %d regs before:\n", drm_crtc_index(crtc));
  337. vc4_crtc_dump_regs(vc4_crtc);
  338. }
  339. /* Reset the PV fifo. */
  340. CRTC_WRITE(PV_CONTROL, 0);
  341. CRTC_WRITE(PV_CONTROL, PV_CONTROL_FIFO_CLR | PV_CONTROL_EN);
  342. CRTC_WRITE(PV_CONTROL, 0);
  343. CRTC_WRITE(PV_HORZA,
  344. VC4_SET_FIELD(mode->htotal - mode->hsync_end,
  345. PV_HORZA_HBP) |
  346. VC4_SET_FIELD(mode->hsync_end - mode->hsync_start,
  347. PV_HORZA_HSYNC));
  348. CRTC_WRITE(PV_HORZB,
  349. VC4_SET_FIELD(mode->hsync_start - mode->hdisplay,
  350. PV_HORZB_HFP) |
  351. VC4_SET_FIELD(mode->hdisplay, PV_HORZB_HACTIVE));
  352. CRTC_WRITE(PV_VERTA,
  353. VC4_SET_FIELD(mode->vtotal - mode->vsync_end,
  354. PV_VERTA_VBP) |
  355. VC4_SET_FIELD(mode->vsync_end - mode->vsync_start,
  356. PV_VERTA_VSYNC));
  357. CRTC_WRITE(PV_VERTB,
  358. VC4_SET_FIELD(mode->vsync_start - mode->vdisplay,
  359. PV_VERTB_VFP) |
  360. VC4_SET_FIELD(vactive, PV_VERTB_VACTIVE));
  361. if (interlace) {
  362. CRTC_WRITE(PV_VERTA_EVEN,
  363. VC4_SET_FIELD(mode->vtotal - mode->vsync_end - 1,
  364. PV_VERTA_VBP) |
  365. VC4_SET_FIELD(mode->vsync_end - mode->vsync_start,
  366. PV_VERTA_VSYNC));
  367. CRTC_WRITE(PV_VERTB_EVEN,
  368. VC4_SET_FIELD(mode->vsync_start - mode->vdisplay,
  369. PV_VERTB_VFP) |
  370. VC4_SET_FIELD(vactive, PV_VERTB_VACTIVE));
  371. }
  372. CRTC_WRITE(PV_HACT_ACT, mode->hdisplay);
  373. CRTC_WRITE(PV_V_CONTROL,
  374. PV_VCONTROL_CONTINUOUS |
  375. (interlace ? PV_VCONTROL_INTERLACE : 0));
  376. CRTC_WRITE(PV_CONTROL,
  377. VC4_SET_FIELD(format, PV_CONTROL_FORMAT) |
  378. VC4_SET_FIELD(vc4_get_fifo_full_level(format),
  379. PV_CONTROL_FIFO_LEVEL) |
  380. PV_CONTROL_CLR_AT_START |
  381. PV_CONTROL_TRIGGER_UNDERFLOW |
  382. PV_CONTROL_WAIT_HSTART |
  383. VC4_SET_FIELD(clock_select, PV_CONTROL_CLK_SELECT) |
  384. PV_CONTROL_FIFO_CLR |
  385. PV_CONTROL_EN);
  386. HVS_WRITE(SCALER_DISPBKGNDX(vc4_crtc->channel),
  387. SCALER_DISPBKGND_AUTOHS |
  388. SCALER_DISPBKGND_GAMMA |
  389. (interlace ? SCALER_DISPBKGND_INTERLACE : 0));
  390. /* Reload the LUT, since the SRAMs would have been disabled if
  391. * all CRTCs had SCALER_DISPBKGND_GAMMA unset at once.
  392. */
  393. vc4_crtc_lut_load(crtc);
  394. if (debug_dump_regs) {
  395. DRM_INFO("CRTC %d regs after:\n", drm_crtc_index(crtc));
  396. vc4_crtc_dump_regs(vc4_crtc);
  397. }
  398. }
  399. static void require_hvs_enabled(struct drm_device *dev)
  400. {
  401. struct vc4_dev *vc4 = to_vc4_dev(dev);
  402. WARN_ON_ONCE((HVS_READ(SCALER_DISPCTRL) & SCALER_DISPCTRL_ENABLE) !=
  403. SCALER_DISPCTRL_ENABLE);
  404. }
  405. static void vc4_crtc_disable(struct drm_crtc *crtc)
  406. {
  407. struct drm_device *dev = crtc->dev;
  408. struct vc4_dev *vc4 = to_vc4_dev(dev);
  409. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  410. u32 chan = vc4_crtc->channel;
  411. int ret;
  412. require_hvs_enabled(dev);
  413. CRTC_WRITE(PV_V_CONTROL,
  414. CRTC_READ(PV_V_CONTROL) & ~PV_VCONTROL_VIDEN);
  415. ret = wait_for(!(CRTC_READ(PV_V_CONTROL) & PV_VCONTROL_VIDEN), 1);
  416. WARN_ONCE(ret, "Timeout waiting for !PV_VCONTROL_VIDEN\n");
  417. if (HVS_READ(SCALER_DISPCTRLX(chan)) &
  418. SCALER_DISPCTRLX_ENABLE) {
  419. HVS_WRITE(SCALER_DISPCTRLX(chan),
  420. SCALER_DISPCTRLX_RESET);
  421. /* While the docs say that reset is self-clearing, it
  422. * seems it doesn't actually.
  423. */
  424. HVS_WRITE(SCALER_DISPCTRLX(chan), 0);
  425. }
  426. /* Once we leave, the scaler should be disabled and its fifo empty. */
  427. WARN_ON_ONCE(HVS_READ(SCALER_DISPCTRLX(chan)) & SCALER_DISPCTRLX_RESET);
  428. WARN_ON_ONCE(VC4_GET_FIELD(HVS_READ(SCALER_DISPSTATX(chan)),
  429. SCALER_DISPSTATX_MODE) !=
  430. SCALER_DISPSTATX_MODE_DISABLED);
  431. WARN_ON_ONCE((HVS_READ(SCALER_DISPSTATX(chan)) &
  432. (SCALER_DISPSTATX_FULL | SCALER_DISPSTATX_EMPTY)) !=
  433. SCALER_DISPSTATX_EMPTY);
  434. }
  435. static void vc4_crtc_enable(struct drm_crtc *crtc)
  436. {
  437. struct drm_device *dev = crtc->dev;
  438. struct vc4_dev *vc4 = to_vc4_dev(dev);
  439. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  440. struct drm_crtc_state *state = crtc->state;
  441. struct drm_display_mode *mode = &state->adjusted_mode;
  442. require_hvs_enabled(dev);
  443. /* Turn on the scaler, which will wait for vstart to start
  444. * compositing.
  445. */
  446. HVS_WRITE(SCALER_DISPCTRLX(vc4_crtc->channel),
  447. VC4_SET_FIELD(mode->hdisplay, SCALER_DISPCTRLX_WIDTH) |
  448. VC4_SET_FIELD(mode->vdisplay, SCALER_DISPCTRLX_HEIGHT) |
  449. SCALER_DISPCTRLX_ENABLE);
  450. /* Turn on the pixel valve, which will emit the vstart signal. */
  451. CRTC_WRITE(PV_V_CONTROL,
  452. CRTC_READ(PV_V_CONTROL) | PV_VCONTROL_VIDEN);
  453. }
  454. static int vc4_crtc_atomic_check(struct drm_crtc *crtc,
  455. struct drm_crtc_state *state)
  456. {
  457. struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(state);
  458. struct drm_device *dev = crtc->dev;
  459. struct vc4_dev *vc4 = to_vc4_dev(dev);
  460. struct drm_plane *plane;
  461. unsigned long flags;
  462. const struct drm_plane_state *plane_state;
  463. u32 dlist_count = 0;
  464. int ret;
  465. /* The pixelvalve can only feed one encoder (and encoders are
  466. * 1:1 with connectors.)
  467. */
  468. if (hweight32(state->connector_mask) > 1)
  469. return -EINVAL;
  470. drm_atomic_crtc_state_for_each_plane_state(plane, plane_state, state)
  471. dlist_count += vc4_plane_dlist_size(plane_state);
  472. dlist_count++; /* Account for SCALER_CTL0_END. */
  473. spin_lock_irqsave(&vc4->hvs->mm_lock, flags);
  474. ret = drm_mm_insert_node(&vc4->hvs->dlist_mm, &vc4_state->mm,
  475. dlist_count, 1, 0);
  476. spin_unlock_irqrestore(&vc4->hvs->mm_lock, flags);
  477. if (ret)
  478. return ret;
  479. return 0;
  480. }
  481. static void vc4_crtc_atomic_flush(struct drm_crtc *crtc,
  482. struct drm_crtc_state *old_state)
  483. {
  484. struct drm_device *dev = crtc->dev;
  485. struct vc4_dev *vc4 = to_vc4_dev(dev);
  486. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  487. struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc->state);
  488. struct drm_plane *plane;
  489. bool debug_dump_regs = false;
  490. u32 __iomem *dlist_start = vc4->hvs->dlist + vc4_state->mm.start;
  491. u32 __iomem *dlist_next = dlist_start;
  492. if (debug_dump_regs) {
  493. DRM_INFO("CRTC %d HVS before:\n", drm_crtc_index(crtc));
  494. vc4_hvs_dump_state(dev);
  495. }
  496. /* Copy all the active planes' dlist contents to the hardware dlist. */
  497. drm_atomic_crtc_for_each_plane(plane, crtc) {
  498. dlist_next += vc4_plane_write_dlist(plane, dlist_next);
  499. }
  500. writel(SCALER_CTL0_END, dlist_next);
  501. dlist_next++;
  502. WARN_ON_ONCE(dlist_next - dlist_start != vc4_state->mm.size);
  503. if (crtc->state->event) {
  504. unsigned long flags;
  505. crtc->state->event->pipe = drm_crtc_index(crtc);
  506. WARN_ON(drm_crtc_vblank_get(crtc) != 0);
  507. spin_lock_irqsave(&dev->event_lock, flags);
  508. vc4_crtc->event = crtc->state->event;
  509. crtc->state->event = NULL;
  510. HVS_WRITE(SCALER_DISPLISTX(vc4_crtc->channel),
  511. vc4_state->mm.start);
  512. spin_unlock_irqrestore(&dev->event_lock, flags);
  513. } else {
  514. HVS_WRITE(SCALER_DISPLISTX(vc4_crtc->channel),
  515. vc4_state->mm.start);
  516. }
  517. if (debug_dump_regs) {
  518. DRM_INFO("CRTC %d HVS after:\n", drm_crtc_index(crtc));
  519. vc4_hvs_dump_state(dev);
  520. }
  521. }
  522. int vc4_enable_vblank(struct drm_device *dev, unsigned int crtc_id)
  523. {
  524. struct vc4_dev *vc4 = to_vc4_dev(dev);
  525. struct vc4_crtc *vc4_crtc = vc4->crtc[crtc_id];
  526. CRTC_WRITE(PV_INTEN, PV_INT_VFP_START);
  527. return 0;
  528. }
  529. void vc4_disable_vblank(struct drm_device *dev, unsigned int crtc_id)
  530. {
  531. struct vc4_dev *vc4 = to_vc4_dev(dev);
  532. struct vc4_crtc *vc4_crtc = vc4->crtc[crtc_id];
  533. CRTC_WRITE(PV_INTEN, 0);
  534. }
  535. static void vc4_crtc_handle_page_flip(struct vc4_crtc *vc4_crtc)
  536. {
  537. struct drm_crtc *crtc = &vc4_crtc->base;
  538. struct drm_device *dev = crtc->dev;
  539. struct vc4_dev *vc4 = to_vc4_dev(dev);
  540. struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc->state);
  541. u32 chan = vc4_crtc->channel;
  542. unsigned long flags;
  543. spin_lock_irqsave(&dev->event_lock, flags);
  544. if (vc4_crtc->event &&
  545. (vc4_state->mm.start == HVS_READ(SCALER_DISPLACTX(chan)))) {
  546. drm_crtc_send_vblank_event(crtc, vc4_crtc->event);
  547. vc4_crtc->event = NULL;
  548. drm_crtc_vblank_put(crtc);
  549. }
  550. spin_unlock_irqrestore(&dev->event_lock, flags);
  551. }
  552. static irqreturn_t vc4_crtc_irq_handler(int irq, void *data)
  553. {
  554. struct vc4_crtc *vc4_crtc = data;
  555. u32 stat = CRTC_READ(PV_INTSTAT);
  556. irqreturn_t ret = IRQ_NONE;
  557. if (stat & PV_INT_VFP_START) {
  558. vc4_crtc->t_vblank = ktime_get();
  559. CRTC_WRITE(PV_INTSTAT, PV_INT_VFP_START);
  560. drm_crtc_handle_vblank(&vc4_crtc->base);
  561. vc4_crtc_handle_page_flip(vc4_crtc);
  562. ret = IRQ_HANDLED;
  563. }
  564. return ret;
  565. }
  566. struct vc4_async_flip_state {
  567. struct drm_crtc *crtc;
  568. struct drm_framebuffer *fb;
  569. struct drm_pending_vblank_event *event;
  570. struct vc4_seqno_cb cb;
  571. };
  572. /* Called when the V3D execution for the BO being flipped to is done, so that
  573. * we can actually update the plane's address to point to it.
  574. */
  575. static void
  576. vc4_async_page_flip_complete(struct vc4_seqno_cb *cb)
  577. {
  578. struct vc4_async_flip_state *flip_state =
  579. container_of(cb, struct vc4_async_flip_state, cb);
  580. struct drm_crtc *crtc = flip_state->crtc;
  581. struct drm_device *dev = crtc->dev;
  582. struct vc4_dev *vc4 = to_vc4_dev(dev);
  583. struct drm_plane *plane = crtc->primary;
  584. vc4_plane_async_set_fb(plane, flip_state->fb);
  585. if (flip_state->event) {
  586. unsigned long flags;
  587. spin_lock_irqsave(&dev->event_lock, flags);
  588. drm_crtc_send_vblank_event(crtc, flip_state->event);
  589. spin_unlock_irqrestore(&dev->event_lock, flags);
  590. }
  591. drm_crtc_vblank_put(crtc);
  592. drm_framebuffer_unreference(flip_state->fb);
  593. kfree(flip_state);
  594. up(&vc4->async_modeset);
  595. }
  596. /* Implements async (non-vblank-synced) page flips.
  597. *
  598. * The page flip ioctl needs to return immediately, so we grab the
  599. * modeset semaphore on the pipe, and queue the address update for
  600. * when V3D is done with the BO being flipped to.
  601. */
  602. static int vc4_async_page_flip(struct drm_crtc *crtc,
  603. struct drm_framebuffer *fb,
  604. struct drm_pending_vblank_event *event,
  605. uint32_t flags)
  606. {
  607. struct drm_device *dev = crtc->dev;
  608. struct vc4_dev *vc4 = to_vc4_dev(dev);
  609. struct drm_plane *plane = crtc->primary;
  610. int ret = 0;
  611. struct vc4_async_flip_state *flip_state;
  612. struct drm_gem_cma_object *cma_bo = drm_fb_cma_get_gem_obj(fb, 0);
  613. struct vc4_bo *bo = to_vc4_bo(&cma_bo->base);
  614. flip_state = kzalloc(sizeof(*flip_state), GFP_KERNEL);
  615. if (!flip_state)
  616. return -ENOMEM;
  617. drm_framebuffer_reference(fb);
  618. flip_state->fb = fb;
  619. flip_state->crtc = crtc;
  620. flip_state->event = event;
  621. /* Make sure all other async modesetes have landed. */
  622. ret = down_interruptible(&vc4->async_modeset);
  623. if (ret) {
  624. drm_framebuffer_unreference(fb);
  625. kfree(flip_state);
  626. return ret;
  627. }
  628. WARN_ON(drm_crtc_vblank_get(crtc) != 0);
  629. /* Immediately update the plane's legacy fb pointer, so that later
  630. * modeset prep sees the state that will be present when the semaphore
  631. * is released.
  632. */
  633. drm_atomic_set_fb_for_plane(plane->state, fb);
  634. plane->fb = fb;
  635. vc4_queue_seqno_cb(dev, &flip_state->cb, bo->seqno,
  636. vc4_async_page_flip_complete);
  637. /* Driver takes ownership of state on successful async commit. */
  638. return 0;
  639. }
  640. static int vc4_page_flip(struct drm_crtc *crtc,
  641. struct drm_framebuffer *fb,
  642. struct drm_pending_vblank_event *event,
  643. uint32_t flags)
  644. {
  645. if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
  646. return vc4_async_page_flip(crtc, fb, event, flags);
  647. else
  648. return drm_atomic_helper_page_flip(crtc, fb, event, flags);
  649. }
  650. static struct drm_crtc_state *vc4_crtc_duplicate_state(struct drm_crtc *crtc)
  651. {
  652. struct vc4_crtc_state *vc4_state;
  653. vc4_state = kzalloc(sizeof(*vc4_state), GFP_KERNEL);
  654. if (!vc4_state)
  655. return NULL;
  656. __drm_atomic_helper_crtc_duplicate_state(crtc, &vc4_state->base);
  657. return &vc4_state->base;
  658. }
  659. static void vc4_crtc_destroy_state(struct drm_crtc *crtc,
  660. struct drm_crtc_state *state)
  661. {
  662. struct vc4_dev *vc4 = to_vc4_dev(crtc->dev);
  663. struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(state);
  664. if (vc4_state->mm.allocated) {
  665. unsigned long flags;
  666. spin_lock_irqsave(&vc4->hvs->mm_lock, flags);
  667. drm_mm_remove_node(&vc4_state->mm);
  668. spin_unlock_irqrestore(&vc4->hvs->mm_lock, flags);
  669. }
  670. __drm_atomic_helper_crtc_destroy_state(state);
  671. }
  672. static const struct drm_crtc_funcs vc4_crtc_funcs = {
  673. .set_config = drm_atomic_helper_set_config,
  674. .destroy = vc4_crtc_destroy,
  675. .page_flip = vc4_page_flip,
  676. .set_property = NULL,
  677. .cursor_set = NULL, /* handled by drm_mode_cursor_universal */
  678. .cursor_move = NULL, /* handled by drm_mode_cursor_universal */
  679. .reset = drm_atomic_helper_crtc_reset,
  680. .atomic_duplicate_state = vc4_crtc_duplicate_state,
  681. .atomic_destroy_state = vc4_crtc_destroy_state,
  682. .gamma_set = vc4_crtc_gamma_set,
  683. };
  684. static const struct drm_crtc_helper_funcs vc4_crtc_helper_funcs = {
  685. .mode_set_nofb = vc4_crtc_mode_set_nofb,
  686. .disable = vc4_crtc_disable,
  687. .enable = vc4_crtc_enable,
  688. .atomic_check = vc4_crtc_atomic_check,
  689. .atomic_flush = vc4_crtc_atomic_flush,
  690. };
  691. static const struct vc4_crtc_data pv0_data = {
  692. .hvs_channel = 0,
  693. .encoder0_type = VC4_ENCODER_TYPE_DSI0,
  694. .encoder1_type = VC4_ENCODER_TYPE_DPI,
  695. };
  696. static const struct vc4_crtc_data pv1_data = {
  697. .hvs_channel = 2,
  698. .encoder0_type = VC4_ENCODER_TYPE_DSI1,
  699. .encoder1_type = VC4_ENCODER_TYPE_SMI,
  700. };
  701. static const struct vc4_crtc_data pv2_data = {
  702. .hvs_channel = 1,
  703. .encoder0_type = VC4_ENCODER_TYPE_VEC,
  704. .encoder1_type = VC4_ENCODER_TYPE_HDMI,
  705. };
  706. static const struct of_device_id vc4_crtc_dt_match[] = {
  707. { .compatible = "brcm,bcm2835-pixelvalve0", .data = &pv0_data },
  708. { .compatible = "brcm,bcm2835-pixelvalve1", .data = &pv1_data },
  709. { .compatible = "brcm,bcm2835-pixelvalve2", .data = &pv2_data },
  710. {}
  711. };
  712. static void vc4_set_crtc_possible_masks(struct drm_device *drm,
  713. struct drm_crtc *crtc)
  714. {
  715. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  716. struct drm_encoder *encoder;
  717. drm_for_each_encoder(encoder, drm) {
  718. struct vc4_encoder *vc4_encoder = to_vc4_encoder(encoder);
  719. if (vc4_encoder->type == vc4_crtc->data->encoder0_type) {
  720. vc4_encoder->clock_select = 0;
  721. encoder->possible_crtcs |= drm_crtc_mask(crtc);
  722. } else if (vc4_encoder->type == vc4_crtc->data->encoder1_type) {
  723. vc4_encoder->clock_select = 1;
  724. encoder->possible_crtcs |= drm_crtc_mask(crtc);
  725. }
  726. }
  727. }
  728. static void
  729. vc4_crtc_get_cob_allocation(struct vc4_crtc *vc4_crtc)
  730. {
  731. struct drm_device *drm = vc4_crtc->base.dev;
  732. struct vc4_dev *vc4 = to_vc4_dev(drm);
  733. u32 dispbase = HVS_READ(SCALER_DISPBASEX(vc4_crtc->channel));
  734. /* Top/base are supposed to be 4-pixel aligned, but the
  735. * Raspberry Pi firmware fills the low bits (which are
  736. * presumably ignored).
  737. */
  738. u32 top = VC4_GET_FIELD(dispbase, SCALER_DISPBASEX_TOP) & ~3;
  739. u32 base = VC4_GET_FIELD(dispbase, SCALER_DISPBASEX_BASE) & ~3;
  740. vc4_crtc->cob_size = top - base + 4;
  741. }
  742. static int vc4_crtc_bind(struct device *dev, struct device *master, void *data)
  743. {
  744. struct platform_device *pdev = to_platform_device(dev);
  745. struct drm_device *drm = dev_get_drvdata(master);
  746. struct vc4_dev *vc4 = to_vc4_dev(drm);
  747. struct vc4_crtc *vc4_crtc;
  748. struct drm_crtc *crtc;
  749. struct drm_plane *primary_plane, *cursor_plane, *destroy_plane, *temp;
  750. const struct of_device_id *match;
  751. int ret, i;
  752. vc4_crtc = devm_kzalloc(dev, sizeof(*vc4_crtc), GFP_KERNEL);
  753. if (!vc4_crtc)
  754. return -ENOMEM;
  755. crtc = &vc4_crtc->base;
  756. match = of_match_device(vc4_crtc_dt_match, dev);
  757. if (!match)
  758. return -ENODEV;
  759. vc4_crtc->data = match->data;
  760. vc4_crtc->regs = vc4_ioremap_regs(pdev, 0);
  761. if (IS_ERR(vc4_crtc->regs))
  762. return PTR_ERR(vc4_crtc->regs);
  763. /* For now, we create just the primary and the legacy cursor
  764. * planes. We should be able to stack more planes on easily,
  765. * but to do that we would need to compute the bandwidth
  766. * requirement of the plane configuration, and reject ones
  767. * that will take too much.
  768. */
  769. primary_plane = vc4_plane_init(drm, DRM_PLANE_TYPE_PRIMARY);
  770. if (IS_ERR(primary_plane)) {
  771. dev_err(dev, "failed to construct primary plane\n");
  772. ret = PTR_ERR(primary_plane);
  773. goto err;
  774. }
  775. drm_crtc_init_with_planes(drm, crtc, primary_plane, NULL,
  776. &vc4_crtc_funcs, NULL);
  777. drm_crtc_helper_add(crtc, &vc4_crtc_helper_funcs);
  778. primary_plane->crtc = crtc;
  779. vc4->crtc[drm_crtc_index(crtc)] = vc4_crtc;
  780. vc4_crtc->channel = vc4_crtc->data->hvs_channel;
  781. drm_mode_crtc_set_gamma_size(crtc, ARRAY_SIZE(vc4_crtc->lut_r));
  782. /* Set up some arbitrary number of planes. We're not limited
  783. * by a set number of physical registers, just the space in
  784. * the HVS (16k) and how small an plane can be (28 bytes).
  785. * However, each plane we set up takes up some memory, and
  786. * increases the cost of looping over planes, which atomic
  787. * modesetting does quite a bit. As a result, we pick a
  788. * modest number of planes to expose, that should hopefully
  789. * still cover any sane usecase.
  790. */
  791. for (i = 0; i < 8; i++) {
  792. struct drm_plane *plane =
  793. vc4_plane_init(drm, DRM_PLANE_TYPE_OVERLAY);
  794. if (IS_ERR(plane))
  795. continue;
  796. plane->possible_crtcs = 1 << drm_crtc_index(crtc);
  797. }
  798. /* Set up the legacy cursor after overlay initialization,
  799. * since we overlay planes on the CRTC in the order they were
  800. * initialized.
  801. */
  802. cursor_plane = vc4_plane_init(drm, DRM_PLANE_TYPE_CURSOR);
  803. if (!IS_ERR(cursor_plane)) {
  804. cursor_plane->possible_crtcs = 1 << drm_crtc_index(crtc);
  805. cursor_plane->crtc = crtc;
  806. crtc->cursor = cursor_plane;
  807. }
  808. vc4_crtc_get_cob_allocation(vc4_crtc);
  809. CRTC_WRITE(PV_INTEN, 0);
  810. CRTC_WRITE(PV_INTSTAT, PV_INT_VFP_START);
  811. ret = devm_request_irq(dev, platform_get_irq(pdev, 0),
  812. vc4_crtc_irq_handler, 0, "vc4 crtc", vc4_crtc);
  813. if (ret)
  814. goto err_destroy_planes;
  815. vc4_set_crtc_possible_masks(drm, crtc);
  816. for (i = 0; i < crtc->gamma_size; i++) {
  817. vc4_crtc->lut_r[i] = i;
  818. vc4_crtc->lut_g[i] = i;
  819. vc4_crtc->lut_b[i] = i;
  820. }
  821. platform_set_drvdata(pdev, vc4_crtc);
  822. return 0;
  823. err_destroy_planes:
  824. list_for_each_entry_safe(destroy_plane, temp,
  825. &drm->mode_config.plane_list, head) {
  826. if (destroy_plane->possible_crtcs == 1 << drm_crtc_index(crtc))
  827. destroy_plane->funcs->destroy(destroy_plane);
  828. }
  829. err:
  830. return ret;
  831. }
  832. static void vc4_crtc_unbind(struct device *dev, struct device *master,
  833. void *data)
  834. {
  835. struct platform_device *pdev = to_platform_device(dev);
  836. struct vc4_crtc *vc4_crtc = dev_get_drvdata(dev);
  837. vc4_crtc_destroy(&vc4_crtc->base);
  838. CRTC_WRITE(PV_INTEN, 0);
  839. platform_set_drvdata(pdev, NULL);
  840. }
  841. static const struct component_ops vc4_crtc_ops = {
  842. .bind = vc4_crtc_bind,
  843. .unbind = vc4_crtc_unbind,
  844. };
  845. static int vc4_crtc_dev_probe(struct platform_device *pdev)
  846. {
  847. return component_add(&pdev->dev, &vc4_crtc_ops);
  848. }
  849. static int vc4_crtc_dev_remove(struct platform_device *pdev)
  850. {
  851. component_del(&pdev->dev, &vc4_crtc_ops);
  852. return 0;
  853. }
  854. struct platform_driver vc4_crtc_driver = {
  855. .probe = vc4_crtc_dev_probe,
  856. .remove = vc4_crtc_dev_remove,
  857. .driver = {
  858. .name = "vc4_crtc",
  859. .of_match_table = vc4_crtc_dt_match,
  860. },
  861. };