malidp_planes.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*
  2. * (C) COPYRIGHT 2016 ARM Limited. All rights reserved.
  3. * Author: Liviu Dudau <Liviu.Dudau@arm.com>
  4. *
  5. * This program is free software and is provided to you under the terms of the
  6. * GNU General Public License version 2 as published by the Free Software
  7. * Foundation, and any use by you of this program is subject to the terms
  8. * of such GNU licence.
  9. *
  10. * ARM Mali DP plane manipulation routines.
  11. */
  12. #include <drm/drmP.h>
  13. #include <drm/drm_atomic.h>
  14. #include <drm/drm_atomic_helper.h>
  15. #include <drm/drm_fb_cma_helper.h>
  16. #include <drm/drm_gem_cma_helper.h>
  17. #include <drm/drm_plane_helper.h>
  18. #include <drm/drm_print.h>
  19. #include "malidp_hw.h"
  20. #include "malidp_drv.h"
  21. /* Layer specific register offsets */
  22. #define MALIDP_LAYER_FORMAT 0x000
  23. #define MALIDP_LAYER_CONTROL 0x004
  24. #define LAYER_ENABLE (1 << 0)
  25. #define LAYER_FLOWCFG_MASK 7
  26. #define LAYER_FLOWCFG(x) (((x) & LAYER_FLOWCFG_MASK) << 1)
  27. #define LAYER_FLOWCFG_SCALE_SE 3
  28. #define LAYER_ROT_OFFSET 8
  29. #define LAYER_H_FLIP (1 << 10)
  30. #define LAYER_V_FLIP (1 << 11)
  31. #define LAYER_ROT_MASK (0xf << 8)
  32. #define LAYER_COMP_MASK (0x3 << 12)
  33. #define LAYER_COMP_PIXEL (0x3 << 12)
  34. #define LAYER_COMP_PLANE (0x2 << 12)
  35. #define LAYER_ALPHA_OFFSET (16)
  36. #define LAYER_ALPHA_MASK (0xff)
  37. #define LAYER_ALPHA(x) (((x) & LAYER_ALPHA_MASK) << LAYER_ALPHA_OFFSET)
  38. #define MALIDP_LAYER_COMPOSE 0x008
  39. #define MALIDP_LAYER_SIZE 0x00c
  40. #define LAYER_H_VAL(x) (((x) & 0x1fff) << 0)
  41. #define LAYER_V_VAL(x) (((x) & 0x1fff) << 16)
  42. #define MALIDP_LAYER_COMP_SIZE 0x010
  43. #define MALIDP_LAYER_OFFSET 0x014
  44. #define MALIDP550_LS_ENABLE 0x01c
  45. #define MALIDP550_LS_R1_IN_SIZE 0x020
  46. /*
  47. * This 4-entry look-up-table is used to determine the full 8-bit alpha value
  48. * for formats with 1- or 2-bit alpha channels.
  49. * We set it to give 100%/0% opacity for 1-bit formats and 100%/66%/33%/0%
  50. * opacity for 2-bit formats.
  51. */
  52. #define MALIDP_ALPHA_LUT 0xffaa5500
  53. static void malidp_de_plane_destroy(struct drm_plane *plane)
  54. {
  55. struct malidp_plane *mp = to_malidp_plane(plane);
  56. drm_plane_cleanup(plane);
  57. kfree(mp);
  58. }
  59. /*
  60. * Replicate what the default ->reset hook does: free the state pointer and
  61. * allocate a new empty object. We just need enough space to store
  62. * a malidp_plane_state instead of a drm_plane_state.
  63. */
  64. static void malidp_plane_reset(struct drm_plane *plane)
  65. {
  66. struct malidp_plane_state *state = to_malidp_plane_state(plane->state);
  67. if (state)
  68. __drm_atomic_helper_plane_destroy_state(&state->base);
  69. kfree(state);
  70. plane->state = NULL;
  71. state = kzalloc(sizeof(*state), GFP_KERNEL);
  72. if (state) {
  73. state->base.plane = plane;
  74. state->base.rotation = DRM_MODE_ROTATE_0;
  75. plane->state = &state->base;
  76. }
  77. }
  78. static struct
  79. drm_plane_state *malidp_duplicate_plane_state(struct drm_plane *plane)
  80. {
  81. struct malidp_plane_state *state, *m_state;
  82. if (!plane->state)
  83. return NULL;
  84. state = kmalloc(sizeof(*state), GFP_KERNEL);
  85. if (!state)
  86. return NULL;
  87. m_state = to_malidp_plane_state(plane->state);
  88. __drm_atomic_helper_plane_duplicate_state(plane, &state->base);
  89. state->rotmem_size = m_state->rotmem_size;
  90. state->format = m_state->format;
  91. state->n_planes = m_state->n_planes;
  92. return &state->base;
  93. }
  94. static void malidp_destroy_plane_state(struct drm_plane *plane,
  95. struct drm_plane_state *state)
  96. {
  97. struct malidp_plane_state *m_state = to_malidp_plane_state(state);
  98. __drm_atomic_helper_plane_destroy_state(state);
  99. kfree(m_state);
  100. }
  101. static void malidp_plane_atomic_print_state(struct drm_printer *p,
  102. const struct drm_plane_state *state)
  103. {
  104. struct malidp_plane_state *ms = to_malidp_plane_state(state);
  105. drm_printf(p, "\trotmem_size=%u\n", ms->rotmem_size);
  106. drm_printf(p, "\tformat_id=%u\n", ms->format);
  107. drm_printf(p, "\tn_planes=%u\n", ms->n_planes);
  108. }
  109. static const struct drm_plane_funcs malidp_de_plane_funcs = {
  110. .update_plane = drm_atomic_helper_update_plane,
  111. .disable_plane = drm_atomic_helper_disable_plane,
  112. .destroy = malidp_de_plane_destroy,
  113. .reset = malidp_plane_reset,
  114. .atomic_duplicate_state = malidp_duplicate_plane_state,
  115. .atomic_destroy_state = malidp_destroy_plane_state,
  116. .atomic_print_state = malidp_plane_atomic_print_state,
  117. };
  118. static int malidp_se_check_scaling(struct malidp_plane *mp,
  119. struct drm_plane_state *state)
  120. {
  121. struct drm_crtc_state *crtc_state =
  122. drm_atomic_get_existing_crtc_state(state->state, state->crtc);
  123. struct malidp_crtc_state *mc;
  124. u32 src_w, src_h;
  125. int ret;
  126. if (!crtc_state)
  127. return -EINVAL;
  128. mc = to_malidp_crtc_state(crtc_state);
  129. ret = drm_atomic_helper_check_plane_state(state, crtc_state,
  130. 0, INT_MAX, true, true);
  131. if (ret)
  132. return ret;
  133. if (state->rotation & MALIDP_ROTATED_MASK) {
  134. src_w = state->src_h >> 16;
  135. src_h = state->src_w >> 16;
  136. } else {
  137. src_w = state->src_w >> 16;
  138. src_h = state->src_h >> 16;
  139. }
  140. if ((state->crtc_w == src_w) && (state->crtc_h == src_h)) {
  141. /* Scaling not necessary for this plane. */
  142. mc->scaled_planes_mask &= ~(mp->layer->id);
  143. return 0;
  144. }
  145. if (mp->layer->id & (DE_SMART | DE_GRAPHICS2))
  146. return -EINVAL;
  147. mc->scaled_planes_mask |= mp->layer->id;
  148. /* Defer scaling requirements calculation to the crtc check. */
  149. return 0;
  150. }
  151. static int malidp_de_plane_check(struct drm_plane *plane,
  152. struct drm_plane_state *state)
  153. {
  154. struct malidp_plane *mp = to_malidp_plane(plane);
  155. struct malidp_plane_state *ms = to_malidp_plane_state(state);
  156. bool rotated = state->rotation & MALIDP_ROTATED_MASK;
  157. struct drm_framebuffer *fb;
  158. int i, ret;
  159. if (!state->crtc || !state->fb)
  160. return 0;
  161. fb = state->fb;
  162. ms->format = malidp_hw_get_format_id(&mp->hwdev->hw->map,
  163. mp->layer->id,
  164. fb->format->format);
  165. if (ms->format == MALIDP_INVALID_FORMAT_ID)
  166. return -EINVAL;
  167. ms->n_planes = fb->format->num_planes;
  168. for (i = 0; i < ms->n_planes; i++) {
  169. u8 alignment = malidp_hw_get_pitch_align(mp->hwdev, rotated);
  170. if (fb->pitches[i] & (alignment - 1)) {
  171. DRM_DEBUG_KMS("Invalid pitch %u for plane %d\n",
  172. fb->pitches[i], i);
  173. return -EINVAL;
  174. }
  175. }
  176. if ((state->crtc_w > mp->hwdev->max_line_size) ||
  177. (state->crtc_h > mp->hwdev->max_line_size) ||
  178. (state->crtc_w < mp->hwdev->min_line_size) ||
  179. (state->crtc_h < mp->hwdev->min_line_size))
  180. return -EINVAL;
  181. /*
  182. * DP550/650 video layers can accept 3 plane formats only if
  183. * fb->pitches[1] == fb->pitches[2] since they don't have a
  184. * third plane stride register.
  185. */
  186. if (ms->n_planes == 3 &&
  187. !(mp->hwdev->hw->features & MALIDP_DEVICE_LV_HAS_3_STRIDES) &&
  188. (state->fb->pitches[1] != state->fb->pitches[2]))
  189. return -EINVAL;
  190. ret = malidp_se_check_scaling(mp, state);
  191. if (ret)
  192. return ret;
  193. /* packed RGB888 / BGR888 can't be rotated or flipped */
  194. if (state->rotation != DRM_MODE_ROTATE_0 &&
  195. (fb->format->format == DRM_FORMAT_RGB888 ||
  196. fb->format->format == DRM_FORMAT_BGR888))
  197. return -EINVAL;
  198. ms->rotmem_size = 0;
  199. if (state->rotation & MALIDP_ROTATED_MASK) {
  200. int val;
  201. val = mp->hwdev->hw->rotmem_required(mp->hwdev, state->crtc_h,
  202. state->crtc_w,
  203. fb->format->format);
  204. if (val < 0)
  205. return val;
  206. ms->rotmem_size = val;
  207. }
  208. return 0;
  209. }
  210. static void malidp_de_set_plane_pitches(struct malidp_plane *mp,
  211. int num_planes, unsigned int pitches[3])
  212. {
  213. int i;
  214. int num_strides = num_planes;
  215. if (!mp->layer->stride_offset)
  216. return;
  217. if (num_planes == 3)
  218. num_strides = (mp->hwdev->hw->features &
  219. MALIDP_DEVICE_LV_HAS_3_STRIDES) ? 3 : 2;
  220. for (i = 0; i < num_strides; ++i)
  221. malidp_hw_write(mp->hwdev, pitches[i],
  222. mp->layer->base +
  223. mp->layer->stride_offset + i * 4);
  224. }
  225. static const s16
  226. malidp_yuv2rgb_coeffs[][DRM_COLOR_RANGE_MAX][MALIDP_COLORADJ_NUM_COEFFS] = {
  227. [DRM_COLOR_YCBCR_BT601][DRM_COLOR_YCBCR_LIMITED_RANGE] = {
  228. 1192, 0, 1634,
  229. 1192, -401, -832,
  230. 1192, 2066, 0,
  231. 64, 512, 512
  232. },
  233. [DRM_COLOR_YCBCR_BT601][DRM_COLOR_YCBCR_FULL_RANGE] = {
  234. 1024, 0, 1436,
  235. 1024, -352, -731,
  236. 1024, 1815, 0,
  237. 0, 512, 512
  238. },
  239. [DRM_COLOR_YCBCR_BT709][DRM_COLOR_YCBCR_LIMITED_RANGE] = {
  240. 1192, 0, 1836,
  241. 1192, -218, -546,
  242. 1192, 2163, 0,
  243. 64, 512, 512
  244. },
  245. [DRM_COLOR_YCBCR_BT709][DRM_COLOR_YCBCR_FULL_RANGE] = {
  246. 1024, 0, 1613,
  247. 1024, -192, -479,
  248. 1024, 1900, 0,
  249. 0, 512, 512
  250. },
  251. [DRM_COLOR_YCBCR_BT2020][DRM_COLOR_YCBCR_LIMITED_RANGE] = {
  252. 1024, 0, 1476,
  253. 1024, -165, -572,
  254. 1024, 1884, 0,
  255. 0, 512, 512
  256. },
  257. [DRM_COLOR_YCBCR_BT2020][DRM_COLOR_YCBCR_FULL_RANGE] = {
  258. 1024, 0, 1510,
  259. 1024, -168, -585,
  260. 1024, 1927, 0,
  261. 0, 512, 512
  262. }
  263. };
  264. static void malidp_de_set_color_encoding(struct malidp_plane *plane,
  265. enum drm_color_encoding enc,
  266. enum drm_color_range range)
  267. {
  268. unsigned int i;
  269. for (i = 0; i < MALIDP_COLORADJ_NUM_COEFFS; i++) {
  270. /* coefficients are signed, two's complement values */
  271. malidp_hw_write(plane->hwdev, malidp_yuv2rgb_coeffs[enc][range][i],
  272. plane->layer->base + plane->layer->yuv2rgb_offset +
  273. i * 4);
  274. }
  275. }
  276. static void malidp_de_plane_update(struct drm_plane *plane,
  277. struct drm_plane_state *old_state)
  278. {
  279. struct malidp_plane *mp;
  280. struct malidp_plane_state *ms = to_malidp_plane_state(plane->state);
  281. u32 src_w, src_h, dest_w, dest_h, val;
  282. int i;
  283. bool format_has_alpha = plane->state->fb->format->has_alpha;
  284. mp = to_malidp_plane(plane);
  285. /* convert src values from Q16 fixed point to integer */
  286. src_w = plane->state->src_w >> 16;
  287. src_h = plane->state->src_h >> 16;
  288. dest_w = plane->state->crtc_w;
  289. dest_h = plane->state->crtc_h;
  290. malidp_hw_write(mp->hwdev, ms->format, mp->layer->base);
  291. for (i = 0; i < ms->n_planes; i++) {
  292. /* calculate the offset for the layer's plane registers */
  293. u16 ptr = mp->layer->ptr + (i << 4);
  294. dma_addr_t fb_addr = drm_fb_cma_get_gem_addr(plane->state->fb,
  295. plane->state, i);
  296. malidp_hw_write(mp->hwdev, lower_32_bits(fb_addr), ptr);
  297. malidp_hw_write(mp->hwdev, upper_32_bits(fb_addr), ptr + 4);
  298. }
  299. malidp_de_set_plane_pitches(mp, ms->n_planes,
  300. plane->state->fb->pitches);
  301. if ((plane->state->color_encoding != old_state->color_encoding) ||
  302. (plane->state->color_range != old_state->color_range))
  303. malidp_de_set_color_encoding(mp, plane->state->color_encoding,
  304. plane->state->color_range);
  305. malidp_hw_write(mp->hwdev, LAYER_H_VAL(src_w) | LAYER_V_VAL(src_h),
  306. mp->layer->base + MALIDP_LAYER_SIZE);
  307. malidp_hw_write(mp->hwdev, LAYER_H_VAL(dest_w) | LAYER_V_VAL(dest_h),
  308. mp->layer->base + MALIDP_LAYER_COMP_SIZE);
  309. malidp_hw_write(mp->hwdev, LAYER_H_VAL(plane->state->crtc_x) |
  310. LAYER_V_VAL(plane->state->crtc_y),
  311. mp->layer->base + MALIDP_LAYER_OFFSET);
  312. if (mp->layer->id == DE_SMART)
  313. malidp_hw_write(mp->hwdev,
  314. LAYER_H_VAL(src_w) | LAYER_V_VAL(src_h),
  315. mp->layer->base + MALIDP550_LS_R1_IN_SIZE);
  316. /* first clear the rotation bits */
  317. val = malidp_hw_read(mp->hwdev, mp->layer->base + MALIDP_LAYER_CONTROL);
  318. val &= ~LAYER_ROT_MASK;
  319. /* setup the rotation and axis flip bits */
  320. if (plane->state->rotation & DRM_MODE_ROTATE_MASK)
  321. val |= ilog2(plane->state->rotation & DRM_MODE_ROTATE_MASK) <<
  322. LAYER_ROT_OFFSET;
  323. if (plane->state->rotation & DRM_MODE_REFLECT_X)
  324. val |= LAYER_H_FLIP;
  325. if (plane->state->rotation & DRM_MODE_REFLECT_Y)
  326. val |= LAYER_V_FLIP;
  327. val &= ~LAYER_COMP_MASK;
  328. if (format_has_alpha) {
  329. /*
  330. * always enable pixel alpha blending until we have a way
  331. * to change blend modes
  332. */
  333. val |= LAYER_COMP_PIXEL;
  334. } else {
  335. /*
  336. * do not enable pixel alpha blending as the color channel
  337. * does not have any alpha information
  338. */
  339. val |= LAYER_COMP_PLANE;
  340. /* Set layer alpha coefficient to 0xff ie fully opaque */
  341. val |= LAYER_ALPHA(0xff);
  342. }
  343. val &= ~LAYER_FLOWCFG(LAYER_FLOWCFG_MASK);
  344. if (plane->state->crtc) {
  345. struct malidp_crtc_state *m =
  346. to_malidp_crtc_state(plane->state->crtc->state);
  347. if (m->scaler_config.scale_enable &&
  348. m->scaler_config.plane_src_id == mp->layer->id)
  349. val |= LAYER_FLOWCFG(LAYER_FLOWCFG_SCALE_SE);
  350. }
  351. /* set the 'enable layer' bit */
  352. val |= LAYER_ENABLE;
  353. malidp_hw_write(mp->hwdev, val,
  354. mp->layer->base + MALIDP_LAYER_CONTROL);
  355. }
  356. static void malidp_de_plane_disable(struct drm_plane *plane,
  357. struct drm_plane_state *state)
  358. {
  359. struct malidp_plane *mp = to_malidp_plane(plane);
  360. malidp_hw_clearbits(mp->hwdev,
  361. LAYER_ENABLE | LAYER_FLOWCFG(LAYER_FLOWCFG_MASK),
  362. mp->layer->base + MALIDP_LAYER_CONTROL);
  363. }
  364. static const struct drm_plane_helper_funcs malidp_de_plane_helper_funcs = {
  365. .atomic_check = malidp_de_plane_check,
  366. .atomic_update = malidp_de_plane_update,
  367. .atomic_disable = malidp_de_plane_disable,
  368. };
  369. int malidp_de_planes_init(struct drm_device *drm)
  370. {
  371. struct malidp_drm *malidp = drm->dev_private;
  372. const struct malidp_hw_regmap *map = &malidp->dev->hw->map;
  373. struct malidp_plane *plane = NULL;
  374. enum drm_plane_type plane_type;
  375. unsigned long crtcs = 1 << drm->mode_config.num_crtc;
  376. unsigned long flags = DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 | DRM_MODE_ROTATE_180 |
  377. DRM_MODE_ROTATE_270 | DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y;
  378. u32 *formats;
  379. int ret, i, j, n;
  380. formats = kcalloc(map->n_pixel_formats, sizeof(*formats), GFP_KERNEL);
  381. if (!formats) {
  382. ret = -ENOMEM;
  383. goto cleanup;
  384. }
  385. for (i = 0; i < map->n_layers; i++) {
  386. u8 id = map->layers[i].id;
  387. plane = kzalloc(sizeof(*plane), GFP_KERNEL);
  388. if (!plane) {
  389. ret = -ENOMEM;
  390. goto cleanup;
  391. }
  392. /* build the list of DRM supported formats based on the map */
  393. for (n = 0, j = 0; j < map->n_pixel_formats; j++) {
  394. if ((map->pixel_formats[j].layer & id) == id)
  395. formats[n++] = map->pixel_formats[j].format;
  396. }
  397. plane_type = (i == 0) ? DRM_PLANE_TYPE_PRIMARY :
  398. DRM_PLANE_TYPE_OVERLAY;
  399. ret = drm_universal_plane_init(drm, &plane->base, crtcs,
  400. &malidp_de_plane_funcs, formats,
  401. n, NULL, plane_type, NULL);
  402. if (ret < 0)
  403. goto cleanup;
  404. drm_plane_helper_add(&plane->base,
  405. &malidp_de_plane_helper_funcs);
  406. plane->hwdev = malidp->dev;
  407. plane->layer = &map->layers[i];
  408. if (id == DE_SMART) {
  409. /*
  410. * Enable the first rectangle in the SMART layer to be
  411. * able to use it as a drm plane.
  412. */
  413. malidp_hw_write(malidp->dev, 1,
  414. plane->layer->base + MALIDP550_LS_ENABLE);
  415. /* Skip the features which the SMART layer doesn't have. */
  416. continue;
  417. }
  418. drm_plane_create_rotation_property(&plane->base, DRM_MODE_ROTATE_0, flags);
  419. malidp_hw_write(malidp->dev, MALIDP_ALPHA_LUT,
  420. plane->layer->base + MALIDP_LAYER_COMPOSE);
  421. /* Attach the YUV->RGB property only to video layers */
  422. if (id & (DE_VIDEO1 | DE_VIDEO2)) {
  423. /* default encoding for YUV->RGB is BT601 NARROW */
  424. enum drm_color_encoding enc = DRM_COLOR_YCBCR_BT601;
  425. enum drm_color_range range = DRM_COLOR_YCBCR_LIMITED_RANGE;
  426. ret = drm_plane_create_color_properties(&plane->base,
  427. BIT(DRM_COLOR_YCBCR_BT601) | \
  428. BIT(DRM_COLOR_YCBCR_BT709) | \
  429. BIT(DRM_COLOR_YCBCR_BT2020),
  430. BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) | \
  431. BIT(DRM_COLOR_YCBCR_FULL_RANGE),
  432. enc, range);
  433. if (!ret)
  434. /* program the HW registers */
  435. malidp_de_set_color_encoding(plane, enc, range);
  436. else
  437. DRM_WARN("Failed to create video layer %d color properties\n", id);
  438. }
  439. }
  440. kfree(formats);
  441. return 0;
  442. cleanup:
  443. kfree(formats);
  444. return ret;
  445. }