vc4_crtc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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. /* Which HVS channel we're using for our CRTC. */
  46. int channel;
  47. u8 lut_r[256];
  48. u8 lut_g[256];
  49. u8 lut_b[256];
  50. struct drm_pending_vblank_event *event;
  51. };
  52. struct vc4_crtc_state {
  53. struct drm_crtc_state base;
  54. /* Dlist area for this CRTC configuration. */
  55. struct drm_mm_node mm;
  56. };
  57. static inline struct vc4_crtc *
  58. to_vc4_crtc(struct drm_crtc *crtc)
  59. {
  60. return (struct vc4_crtc *)crtc;
  61. }
  62. static inline struct vc4_crtc_state *
  63. to_vc4_crtc_state(struct drm_crtc_state *crtc_state)
  64. {
  65. return (struct vc4_crtc_state *)crtc_state;
  66. }
  67. struct vc4_crtc_data {
  68. /* Which channel of the HVS this pixelvalve sources from. */
  69. int hvs_channel;
  70. enum vc4_encoder_type encoder0_type;
  71. enum vc4_encoder_type encoder1_type;
  72. };
  73. #define CRTC_WRITE(offset, val) writel(val, vc4_crtc->regs + (offset))
  74. #define CRTC_READ(offset) readl(vc4_crtc->regs + (offset))
  75. #define CRTC_REG(reg) { reg, #reg }
  76. static const struct {
  77. u32 reg;
  78. const char *name;
  79. } crtc_regs[] = {
  80. CRTC_REG(PV_CONTROL),
  81. CRTC_REG(PV_V_CONTROL),
  82. CRTC_REG(PV_VSYNCD_EVEN),
  83. CRTC_REG(PV_HORZA),
  84. CRTC_REG(PV_HORZB),
  85. CRTC_REG(PV_VERTA),
  86. CRTC_REG(PV_VERTB),
  87. CRTC_REG(PV_VERTA_EVEN),
  88. CRTC_REG(PV_VERTB_EVEN),
  89. CRTC_REG(PV_INTEN),
  90. CRTC_REG(PV_INTSTAT),
  91. CRTC_REG(PV_STAT),
  92. CRTC_REG(PV_HACT_ACT),
  93. };
  94. static void vc4_crtc_dump_regs(struct vc4_crtc *vc4_crtc)
  95. {
  96. int i;
  97. for (i = 0; i < ARRAY_SIZE(crtc_regs); i++) {
  98. DRM_INFO("0x%04x (%s): 0x%08x\n",
  99. crtc_regs[i].reg, crtc_regs[i].name,
  100. CRTC_READ(crtc_regs[i].reg));
  101. }
  102. }
  103. #ifdef CONFIG_DEBUG_FS
  104. int vc4_crtc_debugfs_regs(struct seq_file *m, void *unused)
  105. {
  106. struct drm_info_node *node = (struct drm_info_node *)m->private;
  107. struct drm_device *dev = node->minor->dev;
  108. int crtc_index = (uintptr_t)node->info_ent->data;
  109. struct drm_crtc *crtc;
  110. struct vc4_crtc *vc4_crtc;
  111. int i;
  112. i = 0;
  113. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  114. if (i == crtc_index)
  115. break;
  116. i++;
  117. }
  118. if (!crtc)
  119. return 0;
  120. vc4_crtc = to_vc4_crtc(crtc);
  121. for (i = 0; i < ARRAY_SIZE(crtc_regs); i++) {
  122. seq_printf(m, "%s (0x%04x): 0x%08x\n",
  123. crtc_regs[i].name, crtc_regs[i].reg,
  124. CRTC_READ(crtc_regs[i].reg));
  125. }
  126. return 0;
  127. }
  128. #endif
  129. static void vc4_crtc_destroy(struct drm_crtc *crtc)
  130. {
  131. drm_crtc_cleanup(crtc);
  132. }
  133. static void
  134. vc4_crtc_lut_load(struct drm_crtc *crtc)
  135. {
  136. struct drm_device *dev = crtc->dev;
  137. struct vc4_dev *vc4 = to_vc4_dev(dev);
  138. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  139. u32 i;
  140. /* The LUT memory is laid out with each HVS channel in order,
  141. * each of which takes 256 writes for R, 256 for G, then 256
  142. * for B.
  143. */
  144. HVS_WRITE(SCALER_GAMADDR,
  145. SCALER_GAMADDR_AUTOINC |
  146. (vc4_crtc->channel * 3 * crtc->gamma_size));
  147. for (i = 0; i < crtc->gamma_size; i++)
  148. HVS_WRITE(SCALER_GAMDATA, vc4_crtc->lut_r[i]);
  149. for (i = 0; i < crtc->gamma_size; i++)
  150. HVS_WRITE(SCALER_GAMDATA, vc4_crtc->lut_g[i]);
  151. for (i = 0; i < crtc->gamma_size; i++)
  152. HVS_WRITE(SCALER_GAMDATA, vc4_crtc->lut_b[i]);
  153. }
  154. static int
  155. vc4_crtc_gamma_set(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
  156. uint32_t size)
  157. {
  158. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  159. u32 i;
  160. for (i = 0; i < size; i++) {
  161. vc4_crtc->lut_r[i] = r[i] >> 8;
  162. vc4_crtc->lut_g[i] = g[i] >> 8;
  163. vc4_crtc->lut_b[i] = b[i] >> 8;
  164. }
  165. vc4_crtc_lut_load(crtc);
  166. return 0;
  167. }
  168. static u32 vc4_get_fifo_full_level(u32 format)
  169. {
  170. static const u32 fifo_len_bytes = 64;
  171. static const u32 hvs_latency_pix = 6;
  172. switch (format) {
  173. case PV_CONTROL_FORMAT_DSIV_16:
  174. case PV_CONTROL_FORMAT_DSIC_16:
  175. return fifo_len_bytes - 2 * hvs_latency_pix;
  176. case PV_CONTROL_FORMAT_DSIV_18:
  177. return fifo_len_bytes - 14;
  178. case PV_CONTROL_FORMAT_24:
  179. case PV_CONTROL_FORMAT_DSIV_24:
  180. default:
  181. return fifo_len_bytes - 3 * hvs_latency_pix;
  182. }
  183. }
  184. /*
  185. * Returns the clock select bit for the connector attached to the
  186. * CRTC.
  187. */
  188. static int vc4_get_clock_select(struct drm_crtc *crtc)
  189. {
  190. struct drm_connector *connector;
  191. drm_for_each_connector(connector, crtc->dev) {
  192. if (connector->state->crtc == crtc) {
  193. struct drm_encoder *encoder = connector->encoder;
  194. struct vc4_encoder *vc4_encoder =
  195. to_vc4_encoder(encoder);
  196. return vc4_encoder->clock_select;
  197. }
  198. }
  199. return -1;
  200. }
  201. static void vc4_crtc_mode_set_nofb(struct drm_crtc *crtc)
  202. {
  203. struct drm_device *dev = crtc->dev;
  204. struct vc4_dev *vc4 = to_vc4_dev(dev);
  205. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  206. struct drm_crtc_state *state = crtc->state;
  207. struct drm_display_mode *mode = &state->adjusted_mode;
  208. bool interlace = mode->flags & DRM_MODE_FLAG_INTERLACE;
  209. u32 vactive = (mode->vdisplay >> (interlace ? 1 : 0));
  210. u32 format = PV_CONTROL_FORMAT_24;
  211. bool debug_dump_regs = false;
  212. int clock_select = vc4_get_clock_select(crtc);
  213. if (debug_dump_regs) {
  214. DRM_INFO("CRTC %d regs before:\n", drm_crtc_index(crtc));
  215. vc4_crtc_dump_regs(vc4_crtc);
  216. }
  217. /* Reset the PV fifo. */
  218. CRTC_WRITE(PV_CONTROL, 0);
  219. CRTC_WRITE(PV_CONTROL, PV_CONTROL_FIFO_CLR | PV_CONTROL_EN);
  220. CRTC_WRITE(PV_CONTROL, 0);
  221. CRTC_WRITE(PV_HORZA,
  222. VC4_SET_FIELD(mode->htotal - mode->hsync_end,
  223. PV_HORZA_HBP) |
  224. VC4_SET_FIELD(mode->hsync_end - mode->hsync_start,
  225. PV_HORZA_HSYNC));
  226. CRTC_WRITE(PV_HORZB,
  227. VC4_SET_FIELD(mode->hsync_start - mode->hdisplay,
  228. PV_HORZB_HFP) |
  229. VC4_SET_FIELD(mode->hdisplay, PV_HORZB_HACTIVE));
  230. CRTC_WRITE(PV_VERTA,
  231. VC4_SET_FIELD(mode->vtotal - mode->vsync_end,
  232. PV_VERTA_VBP) |
  233. VC4_SET_FIELD(mode->vsync_end - mode->vsync_start,
  234. PV_VERTA_VSYNC));
  235. CRTC_WRITE(PV_VERTB,
  236. VC4_SET_FIELD(mode->vsync_start - mode->vdisplay,
  237. PV_VERTB_VFP) |
  238. VC4_SET_FIELD(vactive, PV_VERTB_VACTIVE));
  239. if (interlace) {
  240. CRTC_WRITE(PV_VERTA_EVEN,
  241. VC4_SET_FIELD(mode->vtotal - mode->vsync_end - 1,
  242. PV_VERTA_VBP) |
  243. VC4_SET_FIELD(mode->vsync_end - mode->vsync_start,
  244. PV_VERTA_VSYNC));
  245. CRTC_WRITE(PV_VERTB_EVEN,
  246. VC4_SET_FIELD(mode->vsync_start - mode->vdisplay,
  247. PV_VERTB_VFP) |
  248. VC4_SET_FIELD(vactive, PV_VERTB_VACTIVE));
  249. }
  250. CRTC_WRITE(PV_HACT_ACT, mode->hdisplay);
  251. CRTC_WRITE(PV_V_CONTROL,
  252. PV_VCONTROL_CONTINUOUS |
  253. (interlace ? PV_VCONTROL_INTERLACE : 0));
  254. CRTC_WRITE(PV_CONTROL,
  255. VC4_SET_FIELD(format, PV_CONTROL_FORMAT) |
  256. VC4_SET_FIELD(vc4_get_fifo_full_level(format),
  257. PV_CONTROL_FIFO_LEVEL) |
  258. PV_CONTROL_CLR_AT_START |
  259. PV_CONTROL_TRIGGER_UNDERFLOW |
  260. PV_CONTROL_WAIT_HSTART |
  261. VC4_SET_FIELD(clock_select, PV_CONTROL_CLK_SELECT) |
  262. PV_CONTROL_FIFO_CLR |
  263. PV_CONTROL_EN);
  264. HVS_WRITE(SCALER_DISPBKGNDX(vc4_crtc->channel),
  265. SCALER_DISPBKGND_AUTOHS |
  266. SCALER_DISPBKGND_GAMMA |
  267. (interlace ? SCALER_DISPBKGND_INTERLACE : 0));
  268. /* Reload the LUT, since the SRAMs would have been disabled if
  269. * all CRTCs had SCALER_DISPBKGND_GAMMA unset at once.
  270. */
  271. vc4_crtc_lut_load(crtc);
  272. if (debug_dump_regs) {
  273. DRM_INFO("CRTC %d regs after:\n", drm_crtc_index(crtc));
  274. vc4_crtc_dump_regs(vc4_crtc);
  275. }
  276. }
  277. static void require_hvs_enabled(struct drm_device *dev)
  278. {
  279. struct vc4_dev *vc4 = to_vc4_dev(dev);
  280. WARN_ON_ONCE((HVS_READ(SCALER_DISPCTRL) & SCALER_DISPCTRL_ENABLE) !=
  281. SCALER_DISPCTRL_ENABLE);
  282. }
  283. static void vc4_crtc_disable(struct drm_crtc *crtc)
  284. {
  285. struct drm_device *dev = crtc->dev;
  286. struct vc4_dev *vc4 = to_vc4_dev(dev);
  287. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  288. u32 chan = vc4_crtc->channel;
  289. int ret;
  290. require_hvs_enabled(dev);
  291. CRTC_WRITE(PV_V_CONTROL,
  292. CRTC_READ(PV_V_CONTROL) & ~PV_VCONTROL_VIDEN);
  293. ret = wait_for(!(CRTC_READ(PV_V_CONTROL) & PV_VCONTROL_VIDEN), 1);
  294. WARN_ONCE(ret, "Timeout waiting for !PV_VCONTROL_VIDEN\n");
  295. if (HVS_READ(SCALER_DISPCTRLX(chan)) &
  296. SCALER_DISPCTRLX_ENABLE) {
  297. HVS_WRITE(SCALER_DISPCTRLX(chan),
  298. SCALER_DISPCTRLX_RESET);
  299. /* While the docs say that reset is self-clearing, it
  300. * seems it doesn't actually.
  301. */
  302. HVS_WRITE(SCALER_DISPCTRLX(chan), 0);
  303. }
  304. /* Once we leave, the scaler should be disabled and its fifo empty. */
  305. WARN_ON_ONCE(HVS_READ(SCALER_DISPCTRLX(chan)) & SCALER_DISPCTRLX_RESET);
  306. WARN_ON_ONCE(VC4_GET_FIELD(HVS_READ(SCALER_DISPSTATX(chan)),
  307. SCALER_DISPSTATX_MODE) !=
  308. SCALER_DISPSTATX_MODE_DISABLED);
  309. WARN_ON_ONCE((HVS_READ(SCALER_DISPSTATX(chan)) &
  310. (SCALER_DISPSTATX_FULL | SCALER_DISPSTATX_EMPTY)) !=
  311. SCALER_DISPSTATX_EMPTY);
  312. }
  313. static void vc4_crtc_enable(struct drm_crtc *crtc)
  314. {
  315. struct drm_device *dev = crtc->dev;
  316. struct vc4_dev *vc4 = to_vc4_dev(dev);
  317. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  318. struct drm_crtc_state *state = crtc->state;
  319. struct drm_display_mode *mode = &state->adjusted_mode;
  320. require_hvs_enabled(dev);
  321. /* Turn on the scaler, which will wait for vstart to start
  322. * compositing.
  323. */
  324. HVS_WRITE(SCALER_DISPCTRLX(vc4_crtc->channel),
  325. VC4_SET_FIELD(mode->hdisplay, SCALER_DISPCTRLX_WIDTH) |
  326. VC4_SET_FIELD(mode->vdisplay, SCALER_DISPCTRLX_HEIGHT) |
  327. SCALER_DISPCTRLX_ENABLE);
  328. /* Turn on the pixel valve, which will emit the vstart signal. */
  329. CRTC_WRITE(PV_V_CONTROL,
  330. CRTC_READ(PV_V_CONTROL) | PV_VCONTROL_VIDEN);
  331. }
  332. static int vc4_crtc_atomic_check(struct drm_crtc *crtc,
  333. struct drm_crtc_state *state)
  334. {
  335. struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(state);
  336. struct drm_device *dev = crtc->dev;
  337. struct vc4_dev *vc4 = to_vc4_dev(dev);
  338. struct drm_plane *plane;
  339. unsigned long flags;
  340. const struct drm_plane_state *plane_state;
  341. u32 dlist_count = 0;
  342. int ret;
  343. /* The pixelvalve can only feed one encoder (and encoders are
  344. * 1:1 with connectors.)
  345. */
  346. if (hweight32(state->connector_mask) > 1)
  347. return -EINVAL;
  348. drm_atomic_crtc_state_for_each_plane_state(plane, plane_state, state)
  349. dlist_count += vc4_plane_dlist_size(plane_state);
  350. dlist_count++; /* Account for SCALER_CTL0_END. */
  351. spin_lock_irqsave(&vc4->hvs->mm_lock, flags);
  352. ret = drm_mm_insert_node(&vc4->hvs->dlist_mm, &vc4_state->mm,
  353. dlist_count, 1, 0);
  354. spin_unlock_irqrestore(&vc4->hvs->mm_lock, flags);
  355. if (ret)
  356. return ret;
  357. return 0;
  358. }
  359. static void vc4_crtc_atomic_flush(struct drm_crtc *crtc,
  360. struct drm_crtc_state *old_state)
  361. {
  362. struct drm_device *dev = crtc->dev;
  363. struct vc4_dev *vc4 = to_vc4_dev(dev);
  364. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  365. struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc->state);
  366. struct drm_plane *plane;
  367. bool debug_dump_regs = false;
  368. u32 __iomem *dlist_start = vc4->hvs->dlist + vc4_state->mm.start;
  369. u32 __iomem *dlist_next = dlist_start;
  370. if (debug_dump_regs) {
  371. DRM_INFO("CRTC %d HVS before:\n", drm_crtc_index(crtc));
  372. vc4_hvs_dump_state(dev);
  373. }
  374. /* Copy all the active planes' dlist contents to the hardware dlist. */
  375. drm_atomic_crtc_for_each_plane(plane, crtc) {
  376. dlist_next += vc4_plane_write_dlist(plane, dlist_next);
  377. }
  378. writel(SCALER_CTL0_END, dlist_next);
  379. dlist_next++;
  380. WARN_ON_ONCE(dlist_next - dlist_start != vc4_state->mm.size);
  381. HVS_WRITE(SCALER_DISPLISTX(vc4_crtc->channel),
  382. vc4_state->mm.start);
  383. if (debug_dump_regs) {
  384. DRM_INFO("CRTC %d HVS after:\n", drm_crtc_index(crtc));
  385. vc4_hvs_dump_state(dev);
  386. }
  387. if (crtc->state->event) {
  388. unsigned long flags;
  389. crtc->state->event->pipe = drm_crtc_index(crtc);
  390. WARN_ON(drm_crtc_vblank_get(crtc) != 0);
  391. spin_lock_irqsave(&dev->event_lock, flags);
  392. vc4_crtc->event = crtc->state->event;
  393. spin_unlock_irqrestore(&dev->event_lock, flags);
  394. crtc->state->event = NULL;
  395. }
  396. }
  397. int vc4_enable_vblank(struct drm_device *dev, unsigned int crtc_id)
  398. {
  399. struct vc4_dev *vc4 = to_vc4_dev(dev);
  400. struct vc4_crtc *vc4_crtc = vc4->crtc[crtc_id];
  401. CRTC_WRITE(PV_INTEN, PV_INT_VFP_START);
  402. return 0;
  403. }
  404. void vc4_disable_vblank(struct drm_device *dev, unsigned int crtc_id)
  405. {
  406. struct vc4_dev *vc4 = to_vc4_dev(dev);
  407. struct vc4_crtc *vc4_crtc = vc4->crtc[crtc_id];
  408. CRTC_WRITE(PV_INTEN, 0);
  409. }
  410. static void vc4_crtc_handle_page_flip(struct vc4_crtc *vc4_crtc)
  411. {
  412. struct drm_crtc *crtc = &vc4_crtc->base;
  413. struct drm_device *dev = crtc->dev;
  414. unsigned long flags;
  415. spin_lock_irqsave(&dev->event_lock, flags);
  416. if (vc4_crtc->event) {
  417. drm_crtc_send_vblank_event(crtc, vc4_crtc->event);
  418. vc4_crtc->event = NULL;
  419. }
  420. spin_unlock_irqrestore(&dev->event_lock, flags);
  421. }
  422. static irqreturn_t vc4_crtc_irq_handler(int irq, void *data)
  423. {
  424. struct vc4_crtc *vc4_crtc = data;
  425. u32 stat = CRTC_READ(PV_INTSTAT);
  426. irqreturn_t ret = IRQ_NONE;
  427. if (stat & PV_INT_VFP_START) {
  428. CRTC_WRITE(PV_INTSTAT, PV_INT_VFP_START);
  429. drm_crtc_handle_vblank(&vc4_crtc->base);
  430. vc4_crtc_handle_page_flip(vc4_crtc);
  431. ret = IRQ_HANDLED;
  432. }
  433. return ret;
  434. }
  435. struct vc4_async_flip_state {
  436. struct drm_crtc *crtc;
  437. struct drm_framebuffer *fb;
  438. struct drm_pending_vblank_event *event;
  439. struct vc4_seqno_cb cb;
  440. };
  441. /* Called when the V3D execution for the BO being flipped to is done, so that
  442. * we can actually update the plane's address to point to it.
  443. */
  444. static void
  445. vc4_async_page_flip_complete(struct vc4_seqno_cb *cb)
  446. {
  447. struct vc4_async_flip_state *flip_state =
  448. container_of(cb, struct vc4_async_flip_state, cb);
  449. struct drm_crtc *crtc = flip_state->crtc;
  450. struct drm_device *dev = crtc->dev;
  451. struct vc4_dev *vc4 = to_vc4_dev(dev);
  452. struct drm_plane *plane = crtc->primary;
  453. vc4_plane_async_set_fb(plane, flip_state->fb);
  454. if (flip_state->event) {
  455. unsigned long flags;
  456. spin_lock_irqsave(&dev->event_lock, flags);
  457. drm_crtc_send_vblank_event(crtc, flip_state->event);
  458. spin_unlock_irqrestore(&dev->event_lock, flags);
  459. }
  460. drm_framebuffer_unreference(flip_state->fb);
  461. kfree(flip_state);
  462. up(&vc4->async_modeset);
  463. }
  464. /* Implements async (non-vblank-synced) page flips.
  465. *
  466. * The page flip ioctl needs to return immediately, so we grab the
  467. * modeset semaphore on the pipe, and queue the address update for
  468. * when V3D is done with the BO being flipped to.
  469. */
  470. static int vc4_async_page_flip(struct drm_crtc *crtc,
  471. struct drm_framebuffer *fb,
  472. struct drm_pending_vblank_event *event,
  473. uint32_t flags)
  474. {
  475. struct drm_device *dev = crtc->dev;
  476. struct vc4_dev *vc4 = to_vc4_dev(dev);
  477. struct drm_plane *plane = crtc->primary;
  478. int ret = 0;
  479. struct vc4_async_flip_state *flip_state;
  480. struct drm_gem_cma_object *cma_bo = drm_fb_cma_get_gem_obj(fb, 0);
  481. struct vc4_bo *bo = to_vc4_bo(&cma_bo->base);
  482. flip_state = kzalloc(sizeof(*flip_state), GFP_KERNEL);
  483. if (!flip_state)
  484. return -ENOMEM;
  485. drm_framebuffer_reference(fb);
  486. flip_state->fb = fb;
  487. flip_state->crtc = crtc;
  488. flip_state->event = event;
  489. /* Make sure all other async modesetes have landed. */
  490. ret = down_interruptible(&vc4->async_modeset);
  491. if (ret) {
  492. drm_framebuffer_unreference(fb);
  493. kfree(flip_state);
  494. return ret;
  495. }
  496. /* Immediately update the plane's legacy fb pointer, so that later
  497. * modeset prep sees the state that will be present when the semaphore
  498. * is released.
  499. */
  500. drm_atomic_set_fb_for_plane(plane->state, fb);
  501. plane->fb = fb;
  502. vc4_queue_seqno_cb(dev, &flip_state->cb, bo->seqno,
  503. vc4_async_page_flip_complete);
  504. /* Driver takes ownership of state on successful async commit. */
  505. return 0;
  506. }
  507. static int vc4_page_flip(struct drm_crtc *crtc,
  508. struct drm_framebuffer *fb,
  509. struct drm_pending_vblank_event *event,
  510. uint32_t flags)
  511. {
  512. if (flags & DRM_MODE_PAGE_FLIP_ASYNC)
  513. return vc4_async_page_flip(crtc, fb, event, flags);
  514. else
  515. return drm_atomic_helper_page_flip(crtc, fb, event, flags);
  516. }
  517. static struct drm_crtc_state *vc4_crtc_duplicate_state(struct drm_crtc *crtc)
  518. {
  519. struct vc4_crtc_state *vc4_state;
  520. vc4_state = kzalloc(sizeof(*vc4_state), GFP_KERNEL);
  521. if (!vc4_state)
  522. return NULL;
  523. __drm_atomic_helper_crtc_duplicate_state(crtc, &vc4_state->base);
  524. return &vc4_state->base;
  525. }
  526. static void vc4_crtc_destroy_state(struct drm_crtc *crtc,
  527. struct drm_crtc_state *state)
  528. {
  529. struct vc4_dev *vc4 = to_vc4_dev(crtc->dev);
  530. struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(state);
  531. if (vc4_state->mm.allocated) {
  532. unsigned long flags;
  533. spin_lock_irqsave(&vc4->hvs->mm_lock, flags);
  534. drm_mm_remove_node(&vc4_state->mm);
  535. spin_unlock_irqrestore(&vc4->hvs->mm_lock, flags);
  536. }
  537. __drm_atomic_helper_crtc_destroy_state(state);
  538. }
  539. static const struct drm_crtc_funcs vc4_crtc_funcs = {
  540. .set_config = drm_atomic_helper_set_config,
  541. .destroy = vc4_crtc_destroy,
  542. .page_flip = vc4_page_flip,
  543. .set_property = NULL,
  544. .cursor_set = NULL, /* handled by drm_mode_cursor_universal */
  545. .cursor_move = NULL, /* handled by drm_mode_cursor_universal */
  546. .reset = drm_atomic_helper_crtc_reset,
  547. .atomic_duplicate_state = vc4_crtc_duplicate_state,
  548. .atomic_destroy_state = vc4_crtc_destroy_state,
  549. .gamma_set = vc4_crtc_gamma_set,
  550. };
  551. static const struct drm_crtc_helper_funcs vc4_crtc_helper_funcs = {
  552. .mode_set_nofb = vc4_crtc_mode_set_nofb,
  553. .disable = vc4_crtc_disable,
  554. .enable = vc4_crtc_enable,
  555. .atomic_check = vc4_crtc_atomic_check,
  556. .atomic_flush = vc4_crtc_atomic_flush,
  557. };
  558. static const struct vc4_crtc_data pv0_data = {
  559. .hvs_channel = 0,
  560. .encoder0_type = VC4_ENCODER_TYPE_DSI0,
  561. .encoder1_type = VC4_ENCODER_TYPE_DPI,
  562. };
  563. static const struct vc4_crtc_data pv1_data = {
  564. .hvs_channel = 2,
  565. .encoder0_type = VC4_ENCODER_TYPE_DSI1,
  566. .encoder1_type = VC4_ENCODER_TYPE_SMI,
  567. };
  568. static const struct vc4_crtc_data pv2_data = {
  569. .hvs_channel = 1,
  570. .encoder0_type = VC4_ENCODER_TYPE_VEC,
  571. .encoder1_type = VC4_ENCODER_TYPE_HDMI,
  572. };
  573. static const struct of_device_id vc4_crtc_dt_match[] = {
  574. { .compatible = "brcm,bcm2835-pixelvalve0", .data = &pv0_data },
  575. { .compatible = "brcm,bcm2835-pixelvalve1", .data = &pv1_data },
  576. { .compatible = "brcm,bcm2835-pixelvalve2", .data = &pv2_data },
  577. {}
  578. };
  579. static void vc4_set_crtc_possible_masks(struct drm_device *drm,
  580. struct drm_crtc *crtc)
  581. {
  582. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  583. struct drm_encoder *encoder;
  584. drm_for_each_encoder(encoder, drm) {
  585. struct vc4_encoder *vc4_encoder = to_vc4_encoder(encoder);
  586. if (vc4_encoder->type == vc4_crtc->data->encoder0_type) {
  587. vc4_encoder->clock_select = 0;
  588. encoder->possible_crtcs |= drm_crtc_mask(crtc);
  589. } else if (vc4_encoder->type == vc4_crtc->data->encoder1_type) {
  590. vc4_encoder->clock_select = 1;
  591. encoder->possible_crtcs |= drm_crtc_mask(crtc);
  592. }
  593. }
  594. }
  595. static int vc4_crtc_bind(struct device *dev, struct device *master, void *data)
  596. {
  597. struct platform_device *pdev = to_platform_device(dev);
  598. struct drm_device *drm = dev_get_drvdata(master);
  599. struct vc4_dev *vc4 = to_vc4_dev(drm);
  600. struct vc4_crtc *vc4_crtc;
  601. struct drm_crtc *crtc;
  602. struct drm_plane *primary_plane, *cursor_plane, *destroy_plane, *temp;
  603. const struct of_device_id *match;
  604. int ret, i;
  605. vc4_crtc = devm_kzalloc(dev, sizeof(*vc4_crtc), GFP_KERNEL);
  606. if (!vc4_crtc)
  607. return -ENOMEM;
  608. crtc = &vc4_crtc->base;
  609. match = of_match_device(vc4_crtc_dt_match, dev);
  610. if (!match)
  611. return -ENODEV;
  612. vc4_crtc->data = match->data;
  613. vc4_crtc->regs = vc4_ioremap_regs(pdev, 0);
  614. if (IS_ERR(vc4_crtc->regs))
  615. return PTR_ERR(vc4_crtc->regs);
  616. /* For now, we create just the primary and the legacy cursor
  617. * planes. We should be able to stack more planes on easily,
  618. * but to do that we would need to compute the bandwidth
  619. * requirement of the plane configuration, and reject ones
  620. * that will take too much.
  621. */
  622. primary_plane = vc4_plane_init(drm, DRM_PLANE_TYPE_PRIMARY);
  623. if (IS_ERR(primary_plane)) {
  624. dev_err(dev, "failed to construct primary plane\n");
  625. ret = PTR_ERR(primary_plane);
  626. goto err;
  627. }
  628. drm_crtc_init_with_planes(drm, crtc, primary_plane, NULL,
  629. &vc4_crtc_funcs, NULL);
  630. drm_crtc_helper_add(crtc, &vc4_crtc_helper_funcs);
  631. primary_plane->crtc = crtc;
  632. vc4->crtc[drm_crtc_index(crtc)] = vc4_crtc;
  633. vc4_crtc->channel = vc4_crtc->data->hvs_channel;
  634. drm_mode_crtc_set_gamma_size(crtc, ARRAY_SIZE(vc4_crtc->lut_r));
  635. /* Set up some arbitrary number of planes. We're not limited
  636. * by a set number of physical registers, just the space in
  637. * the HVS (16k) and how small an plane can be (28 bytes).
  638. * However, each plane we set up takes up some memory, and
  639. * increases the cost of looping over planes, which atomic
  640. * modesetting does quite a bit. As a result, we pick a
  641. * modest number of planes to expose, that should hopefully
  642. * still cover any sane usecase.
  643. */
  644. for (i = 0; i < 8; i++) {
  645. struct drm_plane *plane =
  646. vc4_plane_init(drm, DRM_PLANE_TYPE_OVERLAY);
  647. if (IS_ERR(plane))
  648. continue;
  649. plane->possible_crtcs = 1 << drm_crtc_index(crtc);
  650. }
  651. /* Set up the legacy cursor after overlay initialization,
  652. * since we overlay planes on the CRTC in the order they were
  653. * initialized.
  654. */
  655. cursor_plane = vc4_plane_init(drm, DRM_PLANE_TYPE_CURSOR);
  656. if (!IS_ERR(cursor_plane)) {
  657. cursor_plane->possible_crtcs = 1 << drm_crtc_index(crtc);
  658. cursor_plane->crtc = crtc;
  659. crtc->cursor = cursor_plane;
  660. }
  661. CRTC_WRITE(PV_INTEN, 0);
  662. CRTC_WRITE(PV_INTSTAT, PV_INT_VFP_START);
  663. ret = devm_request_irq(dev, platform_get_irq(pdev, 0),
  664. vc4_crtc_irq_handler, 0, "vc4 crtc", vc4_crtc);
  665. if (ret)
  666. goto err_destroy_planes;
  667. vc4_set_crtc_possible_masks(drm, crtc);
  668. for (i = 0; i < crtc->gamma_size; i++) {
  669. vc4_crtc->lut_r[i] = i;
  670. vc4_crtc->lut_g[i] = i;
  671. vc4_crtc->lut_b[i] = i;
  672. }
  673. platform_set_drvdata(pdev, vc4_crtc);
  674. return 0;
  675. err_destroy_planes:
  676. list_for_each_entry_safe(destroy_plane, temp,
  677. &drm->mode_config.plane_list, head) {
  678. if (destroy_plane->possible_crtcs == 1 << drm_crtc_index(crtc))
  679. destroy_plane->funcs->destroy(destroy_plane);
  680. }
  681. err:
  682. return ret;
  683. }
  684. static void vc4_crtc_unbind(struct device *dev, struct device *master,
  685. void *data)
  686. {
  687. struct platform_device *pdev = to_platform_device(dev);
  688. struct vc4_crtc *vc4_crtc = dev_get_drvdata(dev);
  689. vc4_crtc_destroy(&vc4_crtc->base);
  690. CRTC_WRITE(PV_INTEN, 0);
  691. platform_set_drvdata(pdev, NULL);
  692. }
  693. static const struct component_ops vc4_crtc_ops = {
  694. .bind = vc4_crtc_bind,
  695. .unbind = vc4_crtc_unbind,
  696. };
  697. static int vc4_crtc_dev_probe(struct platform_device *pdev)
  698. {
  699. return component_add(&pdev->dev, &vc4_crtc_ops);
  700. }
  701. static int vc4_crtc_dev_remove(struct platform_device *pdev)
  702. {
  703. component_del(&pdev->dev, &vc4_crtc_ops);
  704. return 0;
  705. }
  706. struct platform_driver vc4_crtc_driver = {
  707. .probe = vc4_crtc_dev_probe,
  708. .remove = vc4_crtc_dev_remove,
  709. .driver = {
  710. .name = "vc4_crtc",
  711. .of_match_table = vc4_crtc_dt_match,
  712. },
  713. };