vc4_crtc.c 24 KB

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