vc4_txp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright © 2018 Broadcom
  4. *
  5. * Authors:
  6. * Eric Anholt <eric@anholt.net>
  7. * Boris Brezillon <boris.brezillon@bootlin.com>
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/component.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/pm_runtime.h>
  14. #include <drm/drm_atomic.h>
  15. #include <drm/drm_atomic_helper.h>
  16. #include <drm/drm_drv.h>
  17. #include <drm/drm_edid.h>
  18. #include <drm/drm_fb_dma_helper.h>
  19. #include <drm/drm_fourcc.h>
  20. #include <drm/drm_framebuffer.h>
  21. #include <drm/drm_panel.h>
  22. #include <drm/drm_probe_helper.h>
  23. #include <drm/drm_vblank.h>
  24. #include <drm/drm_writeback.h>
  25. #include "vc4_drv.h"
  26. #include "vc4_regs.h"
  27. /* Base address of the output. Raster formats must be 4-byte aligned,
  28. * T and LT must be 16-byte aligned or maybe utile-aligned (docs are
  29. * inconsistent, but probably utile).
  30. */
  31. #define TXP_DST_PTR 0x00
  32. /* Pitch in bytes for raster images, 16-byte aligned. For tiled, it's
  33. * the width in tiles.
  34. */
  35. #define TXP_DST_PITCH 0x04
  36. /* For T-tiled imgaes, DST_PITCH should be the number of tiles wide,
  37. * shifted up.
  38. */
  39. # define TXP_T_TILE_WIDTH_SHIFT 7
  40. /* For LT-tiled images, DST_PITCH should be the number of utiles wide,
  41. * shifted up.
  42. */
  43. # define TXP_LT_TILE_WIDTH_SHIFT 4
  44. /* Pre-rotation width/height of the image. Must match HVS config.
  45. *
  46. * If TFORMAT and 32-bit, limit is 1920 for 32-bit and 3840 to 16-bit
  47. * and width/height must be tile or utile-aligned as appropriate. If
  48. * transposing (rotating), width is limited to 1920.
  49. *
  50. * Height is limited to various numbers between 4088 and 4095. I'd
  51. * just use 4088 to be safe.
  52. */
  53. #define TXP_DIM 0x08
  54. # define TXP_HEIGHT_SHIFT 16
  55. # define TXP_HEIGHT_MASK GENMASK(31, 16)
  56. # define TXP_WIDTH_SHIFT 0
  57. # define TXP_WIDTH_MASK GENMASK(15, 0)
  58. #define TXP_DST_CTRL 0x0c
  59. /* These bits are set to 0x54 */
  60. #define TXP_PILOT_SHIFT 24
  61. #define TXP_PILOT_MASK GENMASK(31, 24)
  62. /* Bits 22-23 are set to 0x01 */
  63. #define TXP_VERSION_SHIFT 22
  64. #define TXP_VERSION_MASK GENMASK(23, 22)
  65. /* Powers down the internal memory. */
  66. # define TXP_POWERDOWN BIT(21)
  67. /* Enables storing the alpha component in 8888/4444, instead of
  68. * filling with ~ALPHA_INVERT.
  69. */
  70. # define TXP_ALPHA_ENABLE BIT(20)
  71. /* 4 bits, each enables stores for a channel in each set of 4 bytes.
  72. * Set to 0xf for normal operation.
  73. */
  74. # define TXP_BYTE_ENABLE_SHIFT 16
  75. # define TXP_BYTE_ENABLE_MASK GENMASK(19, 16)
  76. /* Debug: Generate VSTART again at EOF. */
  77. # define TXP_VSTART_AT_EOF BIT(15)
  78. /* Debug: Terminate the current frame immediately. Stops AXI
  79. * writes.
  80. */
  81. # define TXP_ABORT BIT(14)
  82. # define TXP_DITHER BIT(13)
  83. /* Inverts alpha if TXP_ALPHA_ENABLE, chooses fill value for
  84. * !TXP_ALPHA_ENABLE.
  85. */
  86. # define TXP_ALPHA_INVERT BIT(12)
  87. /* Note: I've listed the channels here in high bit (in byte 3/2/1) to
  88. * low bit (in byte 0) order.
  89. */
  90. # define TXP_FORMAT_SHIFT 8
  91. # define TXP_FORMAT_MASK GENMASK(11, 8)
  92. # define TXP_FORMAT_ABGR4444 0
  93. # define TXP_FORMAT_ARGB4444 1
  94. # define TXP_FORMAT_BGRA4444 2
  95. # define TXP_FORMAT_RGBA4444 3
  96. # define TXP_FORMAT_BGR565 6
  97. # define TXP_FORMAT_RGB565 7
  98. /* 888s are non-rotated, raster-only */
  99. # define TXP_FORMAT_BGR888 8
  100. # define TXP_FORMAT_RGB888 9
  101. # define TXP_FORMAT_ABGR8888 12
  102. # define TXP_FORMAT_ARGB8888 13
  103. # define TXP_FORMAT_BGRA8888 14
  104. # define TXP_FORMAT_RGBA8888 15
  105. /* If TFORMAT is set, generates LT instead of T format. */
  106. # define TXP_LINEAR_UTILE BIT(7)
  107. /* Rotate output by 90 degrees. */
  108. # define TXP_TRANSPOSE BIT(6)
  109. /* Generate a tiled format for V3D. */
  110. # define TXP_TFORMAT BIT(5)
  111. /* Generates some undefined test mode output. */
  112. # define TXP_TEST_MODE BIT(4)
  113. /* Request odd field from HVS. */
  114. # define TXP_FIELD BIT(3)
  115. /* Raise interrupt when idle. */
  116. # define TXP_EI BIT(2)
  117. /* Set when generating a frame, clears when idle. */
  118. # define TXP_BUSY BIT(1)
  119. /* Starts a frame. Self-clearing. */
  120. # define TXP_GO BIT(0)
  121. /* Number of lines received and committed to memory. */
  122. #define TXP_PROGRESS 0x10
  123. #define TXP_READ(offset) \
  124. ({ \
  125. kunit_fail_current_test("Accessing a register in a unit test!\n"); \
  126. readl(txp->regs + (offset)); \
  127. })
  128. #define TXP_WRITE(offset, val) \
  129. do { \
  130. kunit_fail_current_test("Accessing a register in a unit test!\n"); \
  131. writel(val, txp->regs + (offset)); \
  132. } while (0)
  133. struct vc4_txp {
  134. struct vc4_crtc base;
  135. struct platform_device *pdev;
  136. struct vc4_encoder encoder;
  137. struct drm_writeback_connector connector;
  138. void __iomem *regs;
  139. };
  140. #define encoder_to_vc4_txp(_encoder) \
  141. container_of_const(_encoder, struct vc4_txp, encoder.base)
  142. #define connector_to_vc4_txp(_connector) \
  143. container_of_const(_connector, struct vc4_txp, connector.base)
  144. static const struct debugfs_reg32 txp_regs[] = {
  145. VC4_REG32(TXP_DST_PTR),
  146. VC4_REG32(TXP_DST_PITCH),
  147. VC4_REG32(TXP_DIM),
  148. VC4_REG32(TXP_DST_CTRL),
  149. VC4_REG32(TXP_PROGRESS),
  150. };
  151. static int vc4_txp_connector_get_modes(struct drm_connector *connector)
  152. {
  153. struct drm_device *dev = connector->dev;
  154. return drm_add_modes_noedid(connector, dev->mode_config.max_width,
  155. dev->mode_config.max_height);
  156. }
  157. static enum drm_mode_status
  158. vc4_txp_connector_mode_valid(struct drm_connector *connector,
  159. struct drm_display_mode *mode)
  160. {
  161. struct drm_device *dev = connector->dev;
  162. struct drm_mode_config *mode_config = &dev->mode_config;
  163. int w = mode->hdisplay, h = mode->vdisplay;
  164. if (w < mode_config->min_width || w > mode_config->max_width)
  165. return MODE_BAD_HVALUE;
  166. if (h < mode_config->min_height || h > mode_config->max_height)
  167. return MODE_BAD_VVALUE;
  168. return MODE_OK;
  169. }
  170. static const u32 drm_fmts[] = {
  171. DRM_FORMAT_RGB888,
  172. DRM_FORMAT_BGR888,
  173. DRM_FORMAT_XRGB8888,
  174. DRM_FORMAT_XBGR8888,
  175. DRM_FORMAT_ARGB8888,
  176. DRM_FORMAT_ABGR8888,
  177. DRM_FORMAT_RGBX8888,
  178. DRM_FORMAT_BGRX8888,
  179. DRM_FORMAT_RGBA8888,
  180. DRM_FORMAT_BGRA8888,
  181. };
  182. static const u32 txp_fmts[] = {
  183. TXP_FORMAT_RGB888,
  184. TXP_FORMAT_BGR888,
  185. TXP_FORMAT_ARGB8888,
  186. TXP_FORMAT_ABGR8888,
  187. TXP_FORMAT_ARGB8888,
  188. TXP_FORMAT_ABGR8888,
  189. TXP_FORMAT_RGBA8888,
  190. TXP_FORMAT_BGRA8888,
  191. TXP_FORMAT_RGBA8888,
  192. TXP_FORMAT_BGRA8888,
  193. };
  194. static void vc4_txp_armed(struct drm_crtc_state *state)
  195. {
  196. struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(state);
  197. vc4_state->txp_armed = true;
  198. }
  199. static int vc4_txp_connector_atomic_check(struct drm_connector *conn,
  200. struct drm_atomic_state *state)
  201. {
  202. struct drm_connector_state *conn_state;
  203. struct drm_crtc_state *crtc_state;
  204. struct drm_framebuffer *fb;
  205. int i;
  206. conn_state = drm_atomic_get_new_connector_state(state, conn);
  207. if (!conn_state->writeback_job)
  208. return 0;
  209. crtc_state = drm_atomic_get_new_crtc_state(state, conn_state->crtc);
  210. fb = conn_state->writeback_job->fb;
  211. if (fb->width != crtc_state->mode.hdisplay ||
  212. fb->height != crtc_state->mode.vdisplay) {
  213. DRM_DEBUG_KMS("Invalid framebuffer size %ux%u\n",
  214. fb->width, fb->height);
  215. return -EINVAL;
  216. }
  217. for (i = 0; i < ARRAY_SIZE(drm_fmts); i++) {
  218. if (fb->format->format == drm_fmts[i])
  219. break;
  220. }
  221. if (i == ARRAY_SIZE(drm_fmts))
  222. return -EINVAL;
  223. /* Pitch must be aligned on 16 bytes. */
  224. if (fb->pitches[0] & GENMASK(3, 0))
  225. return -EINVAL;
  226. vc4_txp_armed(crtc_state);
  227. return 0;
  228. }
  229. static void vc4_txp_connector_atomic_commit(struct drm_connector *conn,
  230. struct drm_atomic_state *state)
  231. {
  232. struct drm_device *drm = conn->dev;
  233. struct drm_connector_state *conn_state = drm_atomic_get_new_connector_state(state,
  234. conn);
  235. struct vc4_txp *txp = connector_to_vc4_txp(conn);
  236. struct drm_gem_dma_object *gem;
  237. struct drm_display_mode *mode;
  238. struct drm_framebuffer *fb;
  239. u32 ctrl;
  240. int idx;
  241. int i;
  242. if (WARN_ON(!conn_state->writeback_job))
  243. return;
  244. mode = &conn_state->crtc->state->adjusted_mode;
  245. fb = conn_state->writeback_job->fb;
  246. for (i = 0; i < ARRAY_SIZE(drm_fmts); i++) {
  247. if (fb->format->format == drm_fmts[i])
  248. break;
  249. }
  250. if (WARN_ON(i == ARRAY_SIZE(drm_fmts)))
  251. return;
  252. ctrl = TXP_GO | TXP_EI |
  253. VC4_SET_FIELD(0xf, TXP_BYTE_ENABLE) |
  254. VC4_SET_FIELD(txp_fmts[i], TXP_FORMAT);
  255. if (fb->format->has_alpha)
  256. ctrl |= TXP_ALPHA_ENABLE;
  257. else
  258. /*
  259. * If TXP_ALPHA_ENABLE isn't set and TXP_ALPHA_INVERT is, the
  260. * hardware will force the output padding to be 0xff.
  261. */
  262. ctrl |= TXP_ALPHA_INVERT;
  263. if (!drm_dev_enter(drm, &idx))
  264. return;
  265. gem = drm_fb_dma_get_gem_obj(fb, 0);
  266. TXP_WRITE(TXP_DST_PTR, gem->dma_addr + fb->offsets[0]);
  267. TXP_WRITE(TXP_DST_PITCH, fb->pitches[0]);
  268. TXP_WRITE(TXP_DIM,
  269. VC4_SET_FIELD(mode->hdisplay, TXP_WIDTH) |
  270. VC4_SET_FIELD(mode->vdisplay, TXP_HEIGHT));
  271. TXP_WRITE(TXP_DST_CTRL, ctrl);
  272. drm_writeback_queue_job(&txp->connector, conn_state);
  273. drm_dev_exit(idx);
  274. }
  275. static const struct drm_connector_helper_funcs vc4_txp_connector_helper_funcs = {
  276. .get_modes = vc4_txp_connector_get_modes,
  277. .mode_valid = vc4_txp_connector_mode_valid,
  278. .atomic_check = vc4_txp_connector_atomic_check,
  279. .atomic_commit = vc4_txp_connector_atomic_commit,
  280. };
  281. static enum drm_connector_status
  282. vc4_txp_connector_detect(struct drm_connector *connector, bool force)
  283. {
  284. return connector_status_connected;
  285. }
  286. static const struct drm_connector_funcs vc4_txp_connector_funcs = {
  287. .detect = vc4_txp_connector_detect,
  288. .fill_modes = drm_helper_probe_single_connector_modes,
  289. .destroy = drm_connector_cleanup,
  290. .reset = drm_atomic_helper_connector_reset,
  291. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  292. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  293. };
  294. static void vc4_txp_encoder_disable(struct drm_encoder *encoder)
  295. {
  296. struct drm_device *drm = encoder->dev;
  297. struct vc4_txp *txp = encoder_to_vc4_txp(encoder);
  298. int idx;
  299. if (!drm_dev_enter(drm, &idx))
  300. return;
  301. if (TXP_READ(TXP_DST_CTRL) & TXP_BUSY) {
  302. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  303. TXP_WRITE(TXP_DST_CTRL, TXP_ABORT);
  304. while (TXP_READ(TXP_DST_CTRL) & TXP_BUSY &&
  305. time_before(jiffies, timeout))
  306. ;
  307. WARN_ON(TXP_READ(TXP_DST_CTRL) & TXP_BUSY);
  308. }
  309. TXP_WRITE(TXP_DST_CTRL, TXP_POWERDOWN);
  310. drm_dev_exit(idx);
  311. }
  312. static const struct drm_encoder_helper_funcs vc4_txp_encoder_helper_funcs = {
  313. .disable = vc4_txp_encoder_disable,
  314. };
  315. static int vc4_txp_enable_vblank(struct drm_crtc *crtc)
  316. {
  317. return 0;
  318. }
  319. static void vc4_txp_disable_vblank(struct drm_crtc *crtc) {}
  320. static const struct drm_crtc_funcs vc4_txp_crtc_funcs = {
  321. .set_config = drm_atomic_helper_set_config,
  322. .page_flip = vc4_page_flip,
  323. .reset = vc4_crtc_reset,
  324. .atomic_duplicate_state = vc4_crtc_duplicate_state,
  325. .atomic_destroy_state = vc4_crtc_destroy_state,
  326. .enable_vblank = vc4_txp_enable_vblank,
  327. .disable_vblank = vc4_txp_disable_vblank,
  328. .late_register = vc4_crtc_late_register,
  329. };
  330. static int vc4_txp_atomic_check(struct drm_crtc *crtc,
  331. struct drm_atomic_state *state)
  332. {
  333. struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
  334. crtc);
  335. int ret;
  336. ret = vc4_hvs_atomic_check(crtc, state);
  337. if (ret)
  338. return ret;
  339. crtc_state->no_vblank = true;
  340. return 0;
  341. }
  342. static void vc4_txp_atomic_enable(struct drm_crtc *crtc,
  343. struct drm_atomic_state *state)
  344. {
  345. drm_crtc_vblank_on(crtc);
  346. vc4_hvs_atomic_enable(crtc, state);
  347. }
  348. static void vc4_txp_atomic_disable(struct drm_crtc *crtc,
  349. struct drm_atomic_state *state)
  350. {
  351. struct drm_device *dev = crtc->dev;
  352. /* Disable vblank irq handling before crtc is disabled. */
  353. drm_crtc_vblank_off(crtc);
  354. vc4_hvs_atomic_disable(crtc, state);
  355. /*
  356. * Make sure we issue a vblank event after disabling the CRTC if
  357. * someone was waiting it.
  358. */
  359. if (crtc->state->event) {
  360. unsigned long flags;
  361. spin_lock_irqsave(&dev->event_lock, flags);
  362. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  363. crtc->state->event = NULL;
  364. spin_unlock_irqrestore(&dev->event_lock, flags);
  365. }
  366. }
  367. static const struct drm_crtc_helper_funcs vc4_txp_crtc_helper_funcs = {
  368. .atomic_check = vc4_txp_atomic_check,
  369. .atomic_begin = vc4_hvs_atomic_begin,
  370. .atomic_flush = vc4_hvs_atomic_flush,
  371. .atomic_enable = vc4_txp_atomic_enable,
  372. .atomic_disable = vc4_txp_atomic_disable,
  373. };
  374. static irqreturn_t vc4_txp_interrupt(int irq, void *data)
  375. {
  376. struct vc4_txp *txp = data;
  377. struct vc4_crtc *vc4_crtc = &txp->base;
  378. /*
  379. * We don't need to protect the register access using
  380. * drm_dev_enter() there because the interrupt handler lifetime
  381. * is tied to the device itself, and not to the DRM device.
  382. *
  383. * So when the device will be gone, one of the first thing we
  384. * will be doing will be to unregister the interrupt handler,
  385. * and then unregister the DRM device. drm_dev_enter() would
  386. * thus always succeed if we are here.
  387. */
  388. TXP_WRITE(TXP_DST_CTRL, TXP_READ(TXP_DST_CTRL) & ~TXP_EI);
  389. vc4_crtc_handle_vblank(vc4_crtc);
  390. drm_writeback_signal_completion(&txp->connector, 0);
  391. return IRQ_HANDLED;
  392. }
  393. const struct vc4_crtc_data vc4_txp_crtc_data = {
  394. .name = "txp",
  395. .debugfs_name = "txp_regs",
  396. .hvs_available_channels = BIT(2),
  397. .hvs_output = 2,
  398. };
  399. static int vc4_txp_bind(struct device *dev, struct device *master, void *data)
  400. {
  401. struct platform_device *pdev = to_platform_device(dev);
  402. struct drm_device *drm = dev_get_drvdata(master);
  403. struct vc4_encoder *vc4_encoder;
  404. struct drm_encoder *encoder;
  405. struct vc4_crtc *vc4_crtc;
  406. struct vc4_txp *txp;
  407. int ret, irq;
  408. irq = platform_get_irq(pdev, 0);
  409. if (irq < 0)
  410. return irq;
  411. txp = drmm_kzalloc(drm, sizeof(*txp), GFP_KERNEL);
  412. if (!txp)
  413. return -ENOMEM;
  414. txp->pdev = pdev;
  415. txp->regs = vc4_ioremap_regs(pdev, 0);
  416. if (IS_ERR(txp->regs))
  417. return PTR_ERR(txp->regs);
  418. vc4_crtc = &txp->base;
  419. vc4_crtc->regset.base = txp->regs;
  420. vc4_crtc->regset.regs = txp_regs;
  421. vc4_crtc->regset.nregs = ARRAY_SIZE(txp_regs);
  422. ret = vc4_crtc_init(drm, pdev, vc4_crtc, &vc4_txp_crtc_data,
  423. &vc4_txp_crtc_funcs, &vc4_txp_crtc_helper_funcs, true);
  424. if (ret)
  425. return ret;
  426. vc4_encoder = &txp->encoder;
  427. txp->encoder.type = VC4_ENCODER_TYPE_TXP;
  428. encoder = &vc4_encoder->base;
  429. encoder->possible_crtcs = drm_crtc_mask(&vc4_crtc->base);
  430. drm_encoder_helper_add(encoder, &vc4_txp_encoder_helper_funcs);
  431. ret = drmm_encoder_init(drm, encoder, NULL, DRM_MODE_ENCODER_VIRTUAL, NULL);
  432. if (ret)
  433. return ret;
  434. drm_connector_helper_add(&txp->connector.base,
  435. &vc4_txp_connector_helper_funcs);
  436. ret = drm_writeback_connector_init_with_encoder(drm, &txp->connector,
  437. encoder,
  438. &vc4_txp_connector_funcs,
  439. drm_fmts, ARRAY_SIZE(drm_fmts));
  440. if (ret)
  441. return ret;
  442. ret = devm_request_irq(dev, irq, vc4_txp_interrupt, 0,
  443. dev_name(dev), txp);
  444. if (ret)
  445. return ret;
  446. dev_set_drvdata(dev, txp);
  447. return 0;
  448. }
  449. static void vc4_txp_unbind(struct device *dev, struct device *master,
  450. void *data)
  451. {
  452. struct vc4_txp *txp = dev_get_drvdata(dev);
  453. drm_connector_cleanup(&txp->connector.base);
  454. }
  455. static const struct component_ops vc4_txp_ops = {
  456. .bind = vc4_txp_bind,
  457. .unbind = vc4_txp_unbind,
  458. };
  459. static int vc4_txp_probe(struct platform_device *pdev)
  460. {
  461. return component_add(&pdev->dev, &vc4_txp_ops);
  462. }
  463. static void vc4_txp_remove(struct platform_device *pdev)
  464. {
  465. component_del(&pdev->dev, &vc4_txp_ops);
  466. }
  467. static const struct of_device_id vc4_txp_dt_match[] = {
  468. { .compatible = "brcm,bcm2835-txp" },
  469. { /* sentinel */ },
  470. };
  471. struct platform_driver vc4_txp_driver = {
  472. .probe = vc4_txp_probe,
  473. .remove_new = vc4_txp_remove,
  474. .driver = {
  475. .name = "vc4_txp",
  476. .of_match_table = vc4_txp_dt_match,
  477. },
  478. };