malidp_planes.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * (C) COPYRIGHT 2016 ARM Limited. All rights reserved.
  4. * Author: Liviu Dudau <Liviu.Dudau@arm.com>
  5. *
  6. * ARM Mali DP plane manipulation routines.
  7. */
  8. #include <linux/iommu.h>
  9. #include <linux/platform_device.h>
  10. #include <drm/drm_atomic.h>
  11. #include <drm/drm_atomic_helper.h>
  12. #include <drm/drm_blend.h>
  13. #include <drm/drm_drv.h>
  14. #include <drm/drm_fb_dma_helper.h>
  15. #include <drm/drm_fourcc.h>
  16. #include <drm/drm_framebuffer.h>
  17. #include <drm/drm_gem_dma_helper.h>
  18. #include <drm/drm_gem_framebuffer_helper.h>
  19. #include <drm/drm_print.h>
  20. #include "malidp_hw.h"
  21. #include "malidp_drv.h"
  22. /* Layer specific register offsets */
  23. #define MALIDP_LAYER_FORMAT 0x000
  24. #define LAYER_FORMAT_MASK 0x3f
  25. #define MALIDP_LAYER_CONTROL 0x004
  26. #define LAYER_ENABLE (1 << 0)
  27. #define LAYER_FLOWCFG_MASK 7
  28. #define LAYER_FLOWCFG(x) (((x) & LAYER_FLOWCFG_MASK) << 1)
  29. #define LAYER_FLOWCFG_SCALE_SE 3
  30. #define LAYER_ROT_OFFSET 8
  31. #define LAYER_H_FLIP (1 << 10)
  32. #define LAYER_V_FLIP (1 << 11)
  33. #define LAYER_ROT_MASK (0xf << 8)
  34. #define LAYER_COMP_MASK (0x3 << 12)
  35. #define LAYER_COMP_PIXEL (0x3 << 12)
  36. #define LAYER_COMP_PLANE (0x2 << 12)
  37. #define LAYER_PMUL_ENABLE (0x1 << 14)
  38. #define LAYER_ALPHA_OFFSET (16)
  39. #define LAYER_ALPHA_MASK (0xff)
  40. #define LAYER_ALPHA(x) (((x) & LAYER_ALPHA_MASK) << LAYER_ALPHA_OFFSET)
  41. #define MALIDP_LAYER_COMPOSE 0x008
  42. #define MALIDP_LAYER_SIZE 0x00c
  43. #define LAYER_H_VAL(x) (((x) & 0x1fff) << 0)
  44. #define LAYER_V_VAL(x) (((x) & 0x1fff) << 16)
  45. #define MALIDP_LAYER_COMP_SIZE 0x010
  46. #define MALIDP_LAYER_OFFSET 0x014
  47. #define MALIDP550_LS_ENABLE 0x01c
  48. #define MALIDP550_LS_R1_IN_SIZE 0x020
  49. #define MODIFIERS_COUNT_MAX 15
  50. /*
  51. * This 4-entry look-up-table is used to determine the full 8-bit alpha value
  52. * for formats with 1- or 2-bit alpha channels.
  53. * We set it to give 100%/0% opacity for 1-bit formats and 100%/66%/33%/0%
  54. * opacity for 2-bit formats.
  55. */
  56. #define MALIDP_ALPHA_LUT 0xffaa5500
  57. /* page sizes the MMU prefetcher can support */
  58. #define MALIDP_MMU_PREFETCH_PARTIAL_PGSIZES (SZ_4K | SZ_64K)
  59. #define MALIDP_MMU_PREFETCH_FULL_PGSIZES (SZ_1M | SZ_2M)
  60. /* readahead for partial-frame prefetch */
  61. #define MALIDP_MMU_PREFETCH_READAHEAD 8
  62. /*
  63. * Replicate what the default ->reset hook does: free the state pointer and
  64. * allocate a new empty object. We just need enough space to store
  65. * a malidp_plane_state instead of a drm_plane_state.
  66. */
  67. static void malidp_plane_reset(struct drm_plane *plane)
  68. {
  69. struct malidp_plane_state *state = to_malidp_plane_state(plane->state);
  70. if (state)
  71. __drm_atomic_helper_plane_destroy_state(&state->base);
  72. kfree(state);
  73. plane->state = NULL;
  74. state = kzalloc(sizeof(*state), GFP_KERNEL);
  75. if (state)
  76. __drm_atomic_helper_plane_reset(plane, &state->base);
  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. state->mmu_prefetch_mode = m_state->mmu_prefetch_mode;
  93. state->mmu_prefetch_pgsize = m_state->mmu_prefetch_pgsize;
  94. return &state->base;
  95. }
  96. static void malidp_destroy_plane_state(struct drm_plane *plane,
  97. struct drm_plane_state *state)
  98. {
  99. struct malidp_plane_state *m_state = to_malidp_plane_state(state);
  100. __drm_atomic_helper_plane_destroy_state(state);
  101. kfree(m_state);
  102. }
  103. static const char * const prefetch_mode_names[] = {
  104. [MALIDP_PREFETCH_MODE_NONE] = "MMU_PREFETCH_NONE",
  105. [MALIDP_PREFETCH_MODE_PARTIAL] = "MMU_PREFETCH_PARTIAL",
  106. [MALIDP_PREFETCH_MODE_FULL] = "MMU_PREFETCH_FULL",
  107. };
  108. static void malidp_plane_atomic_print_state(struct drm_printer *p,
  109. const struct drm_plane_state *state)
  110. {
  111. struct malidp_plane_state *ms = to_malidp_plane_state(state);
  112. drm_printf(p, "\trotmem_size=%u\n", ms->rotmem_size);
  113. drm_printf(p, "\tformat_id=%u\n", ms->format);
  114. drm_printf(p, "\tn_planes=%u\n", ms->n_planes);
  115. drm_printf(p, "\tmmu_prefetch_mode=%s\n",
  116. prefetch_mode_names[ms->mmu_prefetch_mode]);
  117. drm_printf(p, "\tmmu_prefetch_pgsize=%d\n", ms->mmu_prefetch_pgsize);
  118. }
  119. bool malidp_format_mod_supported(struct drm_device *drm,
  120. u32 format, u64 modifier)
  121. {
  122. const struct drm_format_info *info;
  123. const u64 *modifiers;
  124. struct malidp_drm *malidp = drm_to_malidp(drm);
  125. const struct malidp_hw_regmap *map = &malidp->dev->hw->map;
  126. if (WARN_ON(modifier == DRM_FORMAT_MOD_INVALID))
  127. return false;
  128. /* Some pixel formats are supported without any modifier */
  129. if (modifier == DRM_FORMAT_MOD_LINEAR) {
  130. /*
  131. * However these pixel formats need to be supported with
  132. * modifiers only
  133. */
  134. return !malidp_hw_format_is_afbc_only(format);
  135. }
  136. if (!fourcc_mod_is_vendor(modifier, ARM)) {
  137. DRM_ERROR("Unknown modifier (not Arm)\n");
  138. return false;
  139. }
  140. if (modifier &
  141. ~DRM_FORMAT_MOD_ARM_AFBC(AFBC_MOD_VALID_BITS)) {
  142. DRM_DEBUG_KMS("Unsupported modifiers\n");
  143. return false;
  144. }
  145. modifiers = malidp_format_modifiers;
  146. /* SPLIT buffers must use SPARSE layout */
  147. if (WARN_ON_ONCE((modifier & AFBC_SPLIT) && !(modifier & AFBC_SPARSE)))
  148. return false;
  149. /* CBR only applies to YUV formats, where YTR should be always 0 */
  150. if (WARN_ON_ONCE((modifier & AFBC_CBR) && (modifier & AFBC_YTR)))
  151. return false;
  152. while (*modifiers != DRM_FORMAT_MOD_INVALID) {
  153. if (*modifiers == modifier)
  154. break;
  155. modifiers++;
  156. }
  157. /* return false, if the modifier was not found */
  158. if (*modifiers == DRM_FORMAT_MOD_INVALID) {
  159. DRM_DEBUG_KMS("Unsupported modifier\n");
  160. return false;
  161. }
  162. info = drm_format_info(format);
  163. if (info->num_planes != 1) {
  164. DRM_DEBUG_KMS("AFBC buffers expect one plane\n");
  165. return false;
  166. }
  167. if (malidp_hw_format_is_linear_only(format) == true) {
  168. DRM_DEBUG_KMS("Given format (0x%x) is supported is linear mode only\n",
  169. format);
  170. return false;
  171. }
  172. /*
  173. * RGB formats need to provide YTR modifier and YUV formats should not
  174. * provide YTR modifier.
  175. */
  176. if (!(info->is_yuv) != !!(modifier & AFBC_FORMAT_MOD_YTR)) {
  177. DRM_DEBUG_KMS("AFBC_FORMAT_MOD_YTR is %s for %s formats\n",
  178. info->is_yuv ? "disallowed" : "mandatory",
  179. info->is_yuv ? "YUV" : "RGB");
  180. return false;
  181. }
  182. if (modifier & AFBC_SPLIT) {
  183. if (!info->is_yuv) {
  184. if (info->cpp[0] <= 2) {
  185. DRM_DEBUG_KMS("RGB formats <= 16bpp are not supported with SPLIT\n");
  186. return false;
  187. }
  188. }
  189. if ((info->hsub != 1) || (info->vsub != 1)) {
  190. if (!(format == DRM_FORMAT_YUV420_10BIT &&
  191. (map->features & MALIDP_DEVICE_AFBC_YUV_420_10_SUPPORT_SPLIT))) {
  192. DRM_DEBUG_KMS("Formats which are sub-sampled should never be split\n");
  193. return false;
  194. }
  195. }
  196. }
  197. if (modifier & AFBC_CBR) {
  198. if ((info->hsub == 1) || (info->vsub == 1)) {
  199. DRM_DEBUG_KMS("Formats which are not sub-sampled should not have CBR set\n");
  200. return false;
  201. }
  202. }
  203. return true;
  204. }
  205. static bool malidp_format_mod_supported_per_plane(struct drm_plane *plane,
  206. u32 format, u64 modifier)
  207. {
  208. return malidp_format_mod_supported(plane->dev, format, modifier);
  209. }
  210. static const struct drm_plane_funcs malidp_de_plane_funcs = {
  211. .update_plane = drm_atomic_helper_update_plane,
  212. .disable_plane = drm_atomic_helper_disable_plane,
  213. .reset = malidp_plane_reset,
  214. .atomic_duplicate_state = malidp_duplicate_plane_state,
  215. .atomic_destroy_state = malidp_destroy_plane_state,
  216. .atomic_print_state = malidp_plane_atomic_print_state,
  217. .format_mod_supported = malidp_format_mod_supported_per_plane,
  218. };
  219. static int malidp_se_check_scaling(struct malidp_plane *mp,
  220. struct drm_plane_state *state)
  221. {
  222. struct drm_crtc_state *crtc_state =
  223. drm_atomic_get_existing_crtc_state(state->state, state->crtc);
  224. struct malidp_crtc_state *mc;
  225. u32 src_w, src_h;
  226. int ret;
  227. if (!crtc_state)
  228. return -EINVAL;
  229. mc = to_malidp_crtc_state(crtc_state);
  230. ret = drm_atomic_helper_check_plane_state(state, crtc_state,
  231. 0, INT_MAX, true, true);
  232. if (ret)
  233. return ret;
  234. if (state->rotation & MALIDP_ROTATED_MASK) {
  235. src_w = state->src_h >> 16;
  236. src_h = state->src_w >> 16;
  237. } else {
  238. src_w = state->src_w >> 16;
  239. src_h = state->src_h >> 16;
  240. }
  241. if ((state->crtc_w == src_w) && (state->crtc_h == src_h)) {
  242. /* Scaling not necessary for this plane. */
  243. mc->scaled_planes_mask &= ~(mp->layer->id);
  244. return 0;
  245. }
  246. if (mp->layer->id & (DE_SMART | DE_GRAPHICS2))
  247. return -EINVAL;
  248. mc->scaled_planes_mask |= mp->layer->id;
  249. /* Defer scaling requirements calculation to the crtc check. */
  250. return 0;
  251. }
  252. static u32 malidp_get_pgsize_bitmap(struct malidp_plane *mp)
  253. {
  254. struct iommu_domain *mmu_dom;
  255. mmu_dom = iommu_get_domain_for_dev(mp->base.dev->dev);
  256. if (mmu_dom)
  257. return mmu_dom->pgsize_bitmap;
  258. return 0;
  259. }
  260. /*
  261. * Check if the framebuffer is entirely made up of pages at least pgsize in
  262. * size. Only a heuristic: assumes that each scatterlist entry has been aligned
  263. * to the largest page size smaller than its length and that the MMU maps to
  264. * the largest page size possible.
  265. */
  266. static bool malidp_check_pages_threshold(struct malidp_plane_state *ms,
  267. u32 pgsize)
  268. {
  269. int i;
  270. for (i = 0; i < ms->n_planes; i++) {
  271. struct drm_gem_object *obj;
  272. struct drm_gem_dma_object *dma_obj;
  273. struct sg_table *sgt;
  274. struct scatterlist *sgl;
  275. obj = drm_gem_fb_get_obj(ms->base.fb, i);
  276. dma_obj = to_drm_gem_dma_obj(obj);
  277. if (dma_obj->sgt)
  278. sgt = dma_obj->sgt;
  279. else
  280. sgt = obj->funcs->get_sg_table(obj);
  281. if (IS_ERR(sgt))
  282. return false;
  283. sgl = sgt->sgl;
  284. while (sgl) {
  285. if (sgl->length < pgsize) {
  286. if (!dma_obj->sgt)
  287. kfree(sgt);
  288. return false;
  289. }
  290. sgl = sg_next(sgl);
  291. }
  292. if (!dma_obj->sgt)
  293. kfree(sgt);
  294. }
  295. return true;
  296. }
  297. /*
  298. * Check if it is possible to enable partial-frame MMU prefetch given the
  299. * current format, AFBC state and rotation.
  300. */
  301. static bool malidp_partial_prefetch_supported(u32 format, u64 modifier,
  302. unsigned int rotation)
  303. {
  304. bool afbc, sparse;
  305. /* rotation and horizontal flip not supported for partial prefetch */
  306. if (rotation & (DRM_MODE_ROTATE_90 | DRM_MODE_ROTATE_180 |
  307. DRM_MODE_ROTATE_270 | DRM_MODE_REFLECT_X))
  308. return false;
  309. afbc = modifier & DRM_FORMAT_MOD_ARM_AFBC(0);
  310. sparse = modifier & AFBC_FORMAT_MOD_SPARSE;
  311. switch (format) {
  312. case DRM_FORMAT_ARGB2101010:
  313. case DRM_FORMAT_RGBA1010102:
  314. case DRM_FORMAT_BGRA1010102:
  315. case DRM_FORMAT_ARGB8888:
  316. case DRM_FORMAT_RGBA8888:
  317. case DRM_FORMAT_BGRA8888:
  318. case DRM_FORMAT_XRGB8888:
  319. case DRM_FORMAT_XBGR8888:
  320. case DRM_FORMAT_RGBX8888:
  321. case DRM_FORMAT_BGRX8888:
  322. case DRM_FORMAT_RGB888:
  323. case DRM_FORMAT_RGBA5551:
  324. case DRM_FORMAT_RGB565:
  325. /* always supported */
  326. return true;
  327. case DRM_FORMAT_ABGR2101010:
  328. case DRM_FORMAT_ABGR8888:
  329. case DRM_FORMAT_ABGR1555:
  330. case DRM_FORMAT_BGR565:
  331. /* supported, but if AFBC then must be sparse mode */
  332. return (!afbc) || (afbc && sparse);
  333. case DRM_FORMAT_BGR888:
  334. /* supported, but not for AFBC */
  335. return !afbc;
  336. case DRM_FORMAT_YUYV:
  337. case DRM_FORMAT_UYVY:
  338. case DRM_FORMAT_NV12:
  339. case DRM_FORMAT_YUV420:
  340. /* not supported */
  341. return false;
  342. default:
  343. return false;
  344. }
  345. }
  346. /*
  347. * Select the preferred MMU prefetch mode. Full-frame prefetch is preferred as
  348. * long as the framebuffer is all large pages. Otherwise partial-frame prefetch
  349. * is selected as long as it is supported for the current format. The selected
  350. * page size for prefetch is returned in pgsize_bitmap.
  351. */
  352. static enum mmu_prefetch_mode malidp_mmu_prefetch_select_mode
  353. (struct malidp_plane_state *ms, u32 *pgsize_bitmap)
  354. {
  355. u32 pgsizes;
  356. /* get the full-frame prefetch page size(s) supported by the MMU */
  357. pgsizes = *pgsize_bitmap & MALIDP_MMU_PREFETCH_FULL_PGSIZES;
  358. while (pgsizes) {
  359. u32 largest_pgsize = 1 << __fls(pgsizes);
  360. if (malidp_check_pages_threshold(ms, largest_pgsize)) {
  361. *pgsize_bitmap = largest_pgsize;
  362. return MALIDP_PREFETCH_MODE_FULL;
  363. }
  364. pgsizes -= largest_pgsize;
  365. }
  366. /* get the partial-frame prefetch page size(s) supported by the MMU */
  367. pgsizes = *pgsize_bitmap & MALIDP_MMU_PREFETCH_PARTIAL_PGSIZES;
  368. if (malidp_partial_prefetch_supported(ms->base.fb->format->format,
  369. ms->base.fb->modifier,
  370. ms->base.rotation)) {
  371. /* partial prefetch using the smallest page size */
  372. *pgsize_bitmap = 1 << __ffs(pgsizes);
  373. return MALIDP_PREFETCH_MODE_PARTIAL;
  374. }
  375. *pgsize_bitmap = 0;
  376. return MALIDP_PREFETCH_MODE_NONE;
  377. }
  378. static u32 malidp_calc_mmu_control_value(enum mmu_prefetch_mode mode,
  379. u8 readahead, u8 n_planes, u32 pgsize)
  380. {
  381. u32 mmu_ctrl = 0;
  382. if (mode != MALIDP_PREFETCH_MODE_NONE) {
  383. mmu_ctrl |= MALIDP_MMU_CTRL_EN;
  384. if (mode == MALIDP_PREFETCH_MODE_PARTIAL) {
  385. mmu_ctrl |= MALIDP_MMU_CTRL_MODE;
  386. mmu_ctrl |= MALIDP_MMU_CTRL_PP_NUM_REQ(readahead);
  387. }
  388. if (pgsize == SZ_64K || pgsize == SZ_2M) {
  389. int i;
  390. for (i = 0; i < n_planes; i++)
  391. mmu_ctrl |= MALIDP_MMU_CTRL_PX_PS(i);
  392. }
  393. }
  394. return mmu_ctrl;
  395. }
  396. static void malidp_de_prefetch_settings(struct malidp_plane *mp,
  397. struct malidp_plane_state *ms)
  398. {
  399. if (!mp->layer->mmu_ctrl_offset)
  400. return;
  401. /* get the page sizes supported by the MMU */
  402. ms->mmu_prefetch_pgsize = malidp_get_pgsize_bitmap(mp);
  403. ms->mmu_prefetch_mode =
  404. malidp_mmu_prefetch_select_mode(ms, &ms->mmu_prefetch_pgsize);
  405. }
  406. static int malidp_de_plane_check(struct drm_plane *plane,
  407. struct drm_atomic_state *state)
  408. {
  409. struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
  410. plane);
  411. struct malidp_plane *mp = to_malidp_plane(plane);
  412. struct malidp_plane_state *ms = to_malidp_plane_state(new_plane_state);
  413. bool rotated = new_plane_state->rotation & MALIDP_ROTATED_MASK;
  414. struct drm_framebuffer *fb;
  415. u16 pixel_alpha = new_plane_state->pixel_blend_mode;
  416. int i, ret;
  417. unsigned int block_w, block_h;
  418. if (!new_plane_state->crtc || WARN_ON(!new_plane_state->fb))
  419. return 0;
  420. fb = new_plane_state->fb;
  421. ms->format = malidp_hw_get_format_id(&mp->hwdev->hw->map,
  422. mp->layer->id, fb->format->format,
  423. !!fb->modifier);
  424. if (ms->format == MALIDP_INVALID_FORMAT_ID)
  425. return -EINVAL;
  426. ms->n_planes = fb->format->num_planes;
  427. for (i = 0; i < ms->n_planes; i++) {
  428. u8 alignment = malidp_hw_get_pitch_align(mp->hwdev, rotated);
  429. if (((fb->pitches[i] * drm_format_info_block_height(fb->format, i))
  430. & (alignment - 1)) && !(fb->modifier)) {
  431. DRM_DEBUG_KMS("Invalid pitch %u for plane %d\n",
  432. fb->pitches[i], i);
  433. return -EINVAL;
  434. }
  435. }
  436. block_w = drm_format_info_block_width(fb->format, 0);
  437. block_h = drm_format_info_block_height(fb->format, 0);
  438. if (fb->width % block_w || fb->height % block_h) {
  439. DRM_DEBUG_KMS("Buffer width/height needs to be a multiple of tile sizes");
  440. return -EINVAL;
  441. }
  442. if ((new_plane_state->src_x >> 16) % block_w || (new_plane_state->src_y >> 16) % block_h) {
  443. DRM_DEBUG_KMS("Plane src_x/src_y needs to be a multiple of tile sizes");
  444. return -EINVAL;
  445. }
  446. if ((new_plane_state->crtc_w > mp->hwdev->max_line_size) ||
  447. (new_plane_state->crtc_h > mp->hwdev->max_line_size) ||
  448. (new_plane_state->crtc_w < mp->hwdev->min_line_size) ||
  449. (new_plane_state->crtc_h < mp->hwdev->min_line_size))
  450. return -EINVAL;
  451. /*
  452. * DP550/650 video layers can accept 3 plane formats only if
  453. * fb->pitches[1] == fb->pitches[2] since they don't have a
  454. * third plane stride register.
  455. */
  456. if (ms->n_planes == 3 &&
  457. !(mp->hwdev->hw->features & MALIDP_DEVICE_LV_HAS_3_STRIDES) &&
  458. (new_plane_state->fb->pitches[1] != new_plane_state->fb->pitches[2]))
  459. return -EINVAL;
  460. ret = malidp_se_check_scaling(mp, new_plane_state);
  461. if (ret)
  462. return ret;
  463. /* validate the rotation constraints for each layer */
  464. if (new_plane_state->rotation != DRM_MODE_ROTATE_0) {
  465. if (mp->layer->rot == ROTATE_NONE)
  466. return -EINVAL;
  467. if ((mp->layer->rot == ROTATE_COMPRESSED) && !(fb->modifier))
  468. return -EINVAL;
  469. /*
  470. * packed RGB888 / BGR888 can't be rotated or flipped
  471. * unless they are stored in a compressed way
  472. */
  473. if ((fb->format->format == DRM_FORMAT_RGB888 ||
  474. fb->format->format == DRM_FORMAT_BGR888) && !(fb->modifier))
  475. return -EINVAL;
  476. }
  477. /* SMART layer does not support AFBC */
  478. if (mp->layer->id == DE_SMART && fb->modifier) {
  479. DRM_ERROR("AFBC framebuffer not supported in SMART layer");
  480. return -EINVAL;
  481. }
  482. ms->rotmem_size = 0;
  483. if (new_plane_state->rotation & MALIDP_ROTATED_MASK) {
  484. int val;
  485. val = mp->hwdev->hw->rotmem_required(mp->hwdev, new_plane_state->crtc_w,
  486. new_plane_state->crtc_h,
  487. fb->format->format,
  488. !!(fb->modifier));
  489. if (val < 0)
  490. return val;
  491. ms->rotmem_size = val;
  492. }
  493. /* HW can't support plane + pixel blending */
  494. if ((new_plane_state->alpha != DRM_BLEND_ALPHA_OPAQUE) &&
  495. (pixel_alpha != DRM_MODE_BLEND_PIXEL_NONE) &&
  496. fb->format->has_alpha)
  497. return -EINVAL;
  498. malidp_de_prefetch_settings(mp, ms);
  499. return 0;
  500. }
  501. static void malidp_de_set_plane_pitches(struct malidp_plane *mp,
  502. int num_planes, unsigned int pitches[3])
  503. {
  504. int i;
  505. int num_strides = num_planes;
  506. if (!mp->layer->stride_offset)
  507. return;
  508. if (num_planes == 3)
  509. num_strides = (mp->hwdev->hw->features &
  510. MALIDP_DEVICE_LV_HAS_3_STRIDES) ? 3 : 2;
  511. /*
  512. * The drm convention for pitch is that it needs to cover width * cpp,
  513. * but our hardware wants the pitch/stride to cover all rows included
  514. * in a tile.
  515. */
  516. for (i = 0; i < num_strides; ++i) {
  517. unsigned int block_h = drm_format_info_block_height(mp->base.state->fb->format, i);
  518. malidp_hw_write(mp->hwdev, pitches[i] * block_h,
  519. mp->layer->base +
  520. mp->layer->stride_offset + i * 4);
  521. }
  522. }
  523. static const s16
  524. malidp_yuv2rgb_coeffs[][DRM_COLOR_RANGE_MAX][MALIDP_COLORADJ_NUM_COEFFS] = {
  525. [DRM_COLOR_YCBCR_BT601][DRM_COLOR_YCBCR_LIMITED_RANGE] = {
  526. 1192, 0, 1634,
  527. 1192, -401, -832,
  528. 1192, 2066, 0,
  529. 64, 512, 512
  530. },
  531. [DRM_COLOR_YCBCR_BT601][DRM_COLOR_YCBCR_FULL_RANGE] = {
  532. 1024, 0, 1436,
  533. 1024, -352, -731,
  534. 1024, 1815, 0,
  535. 0, 512, 512
  536. },
  537. [DRM_COLOR_YCBCR_BT709][DRM_COLOR_YCBCR_LIMITED_RANGE] = {
  538. 1192, 0, 1836,
  539. 1192, -218, -546,
  540. 1192, 2163, 0,
  541. 64, 512, 512
  542. },
  543. [DRM_COLOR_YCBCR_BT709][DRM_COLOR_YCBCR_FULL_RANGE] = {
  544. 1024, 0, 1613,
  545. 1024, -192, -479,
  546. 1024, 1900, 0,
  547. 0, 512, 512
  548. },
  549. [DRM_COLOR_YCBCR_BT2020][DRM_COLOR_YCBCR_LIMITED_RANGE] = {
  550. 1024, 0, 1476,
  551. 1024, -165, -572,
  552. 1024, 1884, 0,
  553. 0, 512, 512
  554. },
  555. [DRM_COLOR_YCBCR_BT2020][DRM_COLOR_YCBCR_FULL_RANGE] = {
  556. 1024, 0, 1510,
  557. 1024, -168, -585,
  558. 1024, 1927, 0,
  559. 0, 512, 512
  560. }
  561. };
  562. static void malidp_de_set_color_encoding(struct malidp_plane *plane,
  563. enum drm_color_encoding enc,
  564. enum drm_color_range range)
  565. {
  566. unsigned int i;
  567. for (i = 0; i < MALIDP_COLORADJ_NUM_COEFFS; i++) {
  568. /* coefficients are signed, two's complement values */
  569. malidp_hw_write(plane->hwdev, malidp_yuv2rgb_coeffs[enc][range][i],
  570. plane->layer->base + plane->layer->yuv2rgb_offset +
  571. i * 4);
  572. }
  573. }
  574. static void malidp_de_set_mmu_control(struct malidp_plane *mp,
  575. struct malidp_plane_state *ms)
  576. {
  577. u32 mmu_ctrl;
  578. /* check hardware supports MMU prefetch */
  579. if (!mp->layer->mmu_ctrl_offset)
  580. return;
  581. mmu_ctrl = malidp_calc_mmu_control_value(ms->mmu_prefetch_mode,
  582. MALIDP_MMU_PREFETCH_READAHEAD,
  583. ms->n_planes,
  584. ms->mmu_prefetch_pgsize);
  585. malidp_hw_write(mp->hwdev, mmu_ctrl,
  586. mp->layer->base + mp->layer->mmu_ctrl_offset);
  587. }
  588. static void malidp_set_plane_base_addr(struct drm_framebuffer *fb,
  589. struct malidp_plane *mp,
  590. int plane_index)
  591. {
  592. dma_addr_t dma_addr;
  593. u16 ptr;
  594. struct drm_plane *plane = &mp->base;
  595. bool afbc = fb->modifier ? true : false;
  596. ptr = mp->layer->ptr + (plane_index << 4);
  597. /*
  598. * drm_fb_dma_get_gem_addr() alters the physical base address of the
  599. * framebuffer as per the plane's src_x, src_y co-ordinates (ie to
  600. * take care of source cropping).
  601. * For AFBC, this is not needed as the cropping is handled by _AD_CROP_H
  602. * and _AD_CROP_V registers.
  603. */
  604. if (!afbc) {
  605. dma_addr = drm_fb_dma_get_gem_addr(fb, plane->state,
  606. plane_index);
  607. } else {
  608. struct drm_gem_dma_object *obj;
  609. obj = drm_fb_dma_get_gem_obj(fb, plane_index);
  610. if (WARN_ON(!obj))
  611. return;
  612. dma_addr = obj->dma_addr;
  613. }
  614. malidp_hw_write(mp->hwdev, lower_32_bits(dma_addr), ptr);
  615. malidp_hw_write(mp->hwdev, upper_32_bits(dma_addr), ptr + 4);
  616. }
  617. static void malidp_de_set_plane_afbc(struct drm_plane *plane)
  618. {
  619. struct malidp_plane *mp;
  620. u32 src_w, src_h, val = 0, src_x, src_y;
  621. struct drm_framebuffer *fb = plane->state->fb;
  622. mp = to_malidp_plane(plane);
  623. /* no afbc_decoder_offset means AFBC is not supported on this plane */
  624. if (!mp->layer->afbc_decoder_offset)
  625. return;
  626. if (!fb->modifier) {
  627. malidp_hw_write(mp->hwdev, 0, mp->layer->afbc_decoder_offset);
  628. return;
  629. }
  630. /* convert src values from Q16 fixed point to integer */
  631. src_w = plane->state->src_w >> 16;
  632. src_h = plane->state->src_h >> 16;
  633. src_x = plane->state->src_x >> 16;
  634. src_y = plane->state->src_y >> 16;
  635. val = ((fb->width - (src_x + src_w)) << MALIDP_AD_CROP_RIGHT_OFFSET) |
  636. src_x;
  637. malidp_hw_write(mp->hwdev, val,
  638. mp->layer->afbc_decoder_offset + MALIDP_AD_CROP_H);
  639. val = ((fb->height - (src_y + src_h)) << MALIDP_AD_CROP_BOTTOM_OFFSET) |
  640. src_y;
  641. malidp_hw_write(mp->hwdev, val,
  642. mp->layer->afbc_decoder_offset + MALIDP_AD_CROP_V);
  643. val = MALIDP_AD_EN;
  644. if (fb->modifier & AFBC_FORMAT_MOD_SPLIT)
  645. val |= MALIDP_AD_BS;
  646. if (fb->modifier & AFBC_FORMAT_MOD_YTR)
  647. val |= MALIDP_AD_YTR;
  648. malidp_hw_write(mp->hwdev, val, mp->layer->afbc_decoder_offset);
  649. }
  650. static void malidp_de_plane_update(struct drm_plane *plane,
  651. struct drm_atomic_state *state)
  652. {
  653. struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
  654. plane);
  655. struct malidp_plane *mp;
  656. struct malidp_plane_state *ms = to_malidp_plane_state(plane->state);
  657. struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
  658. plane);
  659. u16 pixel_alpha = new_state->pixel_blend_mode;
  660. u8 plane_alpha = new_state->alpha >> 8;
  661. u32 src_w, src_h, dest_w, dest_h, val;
  662. int i;
  663. struct drm_framebuffer *fb = plane->state->fb;
  664. mp = to_malidp_plane(plane);
  665. /*
  666. * For AFBC framebuffer, use the framebuffer width and height for
  667. * configuring layer input size register.
  668. */
  669. if (fb->modifier) {
  670. src_w = fb->width;
  671. src_h = fb->height;
  672. } else {
  673. /* convert src values from Q16 fixed point to integer */
  674. src_w = new_state->src_w >> 16;
  675. src_h = new_state->src_h >> 16;
  676. }
  677. dest_w = new_state->crtc_w;
  678. dest_h = new_state->crtc_h;
  679. val = malidp_hw_read(mp->hwdev, mp->layer->base);
  680. val = (val & ~LAYER_FORMAT_MASK) | ms->format;
  681. malidp_hw_write(mp->hwdev, val, mp->layer->base);
  682. for (i = 0; i < ms->n_planes; i++)
  683. malidp_set_plane_base_addr(fb, mp, i);
  684. malidp_de_set_mmu_control(mp, ms);
  685. malidp_de_set_plane_pitches(mp, ms->n_planes,
  686. new_state->fb->pitches);
  687. if ((plane->state->color_encoding != old_state->color_encoding) ||
  688. (plane->state->color_range != old_state->color_range))
  689. malidp_de_set_color_encoding(mp, plane->state->color_encoding,
  690. plane->state->color_range);
  691. malidp_hw_write(mp->hwdev, LAYER_H_VAL(src_w) | LAYER_V_VAL(src_h),
  692. mp->layer->base + MALIDP_LAYER_SIZE);
  693. malidp_hw_write(mp->hwdev, LAYER_H_VAL(dest_w) | LAYER_V_VAL(dest_h),
  694. mp->layer->base + MALIDP_LAYER_COMP_SIZE);
  695. malidp_hw_write(mp->hwdev, LAYER_H_VAL(new_state->crtc_x) |
  696. LAYER_V_VAL(new_state->crtc_y),
  697. mp->layer->base + MALIDP_LAYER_OFFSET);
  698. if (mp->layer->id == DE_SMART) {
  699. /*
  700. * Enable the first rectangle in the SMART layer to be
  701. * able to use it as a drm plane.
  702. */
  703. malidp_hw_write(mp->hwdev, 1,
  704. mp->layer->base + MALIDP550_LS_ENABLE);
  705. malidp_hw_write(mp->hwdev,
  706. LAYER_H_VAL(src_w) | LAYER_V_VAL(src_h),
  707. mp->layer->base + MALIDP550_LS_R1_IN_SIZE);
  708. }
  709. malidp_de_set_plane_afbc(plane);
  710. /* first clear the rotation bits */
  711. val = malidp_hw_read(mp->hwdev, mp->layer->base + MALIDP_LAYER_CONTROL);
  712. val &= ~LAYER_ROT_MASK;
  713. /* setup the rotation and axis flip bits */
  714. if (new_state->rotation & DRM_MODE_ROTATE_MASK)
  715. val |= ilog2(plane->state->rotation & DRM_MODE_ROTATE_MASK) <<
  716. LAYER_ROT_OFFSET;
  717. if (new_state->rotation & DRM_MODE_REFLECT_X)
  718. val |= LAYER_H_FLIP;
  719. if (new_state->rotation & DRM_MODE_REFLECT_Y)
  720. val |= LAYER_V_FLIP;
  721. val &= ~(LAYER_COMP_MASK | LAYER_PMUL_ENABLE | LAYER_ALPHA(0xff));
  722. if (new_state->alpha != DRM_BLEND_ALPHA_OPAQUE) {
  723. val |= LAYER_COMP_PLANE;
  724. } else if (new_state->fb->format->has_alpha) {
  725. /* We only care about blend mode if the format has alpha */
  726. switch (pixel_alpha) {
  727. case DRM_MODE_BLEND_PREMULTI:
  728. val |= LAYER_COMP_PIXEL | LAYER_PMUL_ENABLE;
  729. break;
  730. case DRM_MODE_BLEND_COVERAGE:
  731. val |= LAYER_COMP_PIXEL;
  732. break;
  733. }
  734. }
  735. val |= LAYER_ALPHA(plane_alpha);
  736. val &= ~LAYER_FLOWCFG(LAYER_FLOWCFG_MASK);
  737. if (new_state->crtc) {
  738. struct malidp_crtc_state *m =
  739. to_malidp_crtc_state(new_state->crtc->state);
  740. if (m->scaler_config.scale_enable &&
  741. m->scaler_config.plane_src_id == mp->layer->id)
  742. val |= LAYER_FLOWCFG(LAYER_FLOWCFG_SCALE_SE);
  743. }
  744. /* set the 'enable layer' bit */
  745. val |= LAYER_ENABLE;
  746. malidp_hw_write(mp->hwdev, val,
  747. mp->layer->base + MALIDP_LAYER_CONTROL);
  748. }
  749. static void malidp_de_plane_disable(struct drm_plane *plane,
  750. struct drm_atomic_state *state)
  751. {
  752. struct malidp_plane *mp = to_malidp_plane(plane);
  753. malidp_hw_clearbits(mp->hwdev,
  754. LAYER_ENABLE | LAYER_FLOWCFG(LAYER_FLOWCFG_MASK),
  755. mp->layer->base + MALIDP_LAYER_CONTROL);
  756. }
  757. static const struct drm_plane_helper_funcs malidp_de_plane_helper_funcs = {
  758. .atomic_check = malidp_de_plane_check,
  759. .atomic_update = malidp_de_plane_update,
  760. .atomic_disable = malidp_de_plane_disable,
  761. };
  762. static const uint64_t linear_only_modifiers[] = {
  763. DRM_FORMAT_MOD_LINEAR,
  764. DRM_FORMAT_MOD_INVALID
  765. };
  766. int malidp_de_planes_init(struct drm_device *drm)
  767. {
  768. struct malidp_drm *malidp = drm_to_malidp(drm);
  769. const struct malidp_hw_regmap *map = &malidp->dev->hw->map;
  770. struct malidp_plane *plane = NULL;
  771. enum drm_plane_type plane_type;
  772. unsigned long crtcs = BIT(drm->mode_config.num_crtc);
  773. unsigned long flags = DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 | DRM_MODE_ROTATE_180 |
  774. DRM_MODE_ROTATE_270 | DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y;
  775. unsigned int blend_caps = BIT(DRM_MODE_BLEND_PIXEL_NONE) |
  776. BIT(DRM_MODE_BLEND_PREMULTI) |
  777. BIT(DRM_MODE_BLEND_COVERAGE);
  778. u32 *formats;
  779. int ret, i = 0, j = 0, n;
  780. u64 supported_modifiers[MODIFIERS_COUNT_MAX];
  781. const u64 *modifiers;
  782. modifiers = malidp_format_modifiers;
  783. if (!(map->features & MALIDP_DEVICE_AFBC_SUPPORT_SPLIT)) {
  784. /*
  785. * Since our hardware does not support SPLIT, so build the list
  786. * of supported modifiers excluding SPLIT ones.
  787. */
  788. while (*modifiers != DRM_FORMAT_MOD_INVALID) {
  789. if (!(*modifiers & AFBC_SPLIT))
  790. supported_modifiers[j++] = *modifiers;
  791. modifiers++;
  792. }
  793. supported_modifiers[j++] = DRM_FORMAT_MOD_INVALID;
  794. modifiers = supported_modifiers;
  795. }
  796. formats = kcalloc(map->n_pixel_formats, sizeof(*formats), GFP_KERNEL);
  797. if (!formats) {
  798. ret = -ENOMEM;
  799. goto cleanup;
  800. }
  801. for (i = 0; i < map->n_layers; i++) {
  802. u8 id = map->layers[i].id;
  803. /* build the list of DRM supported formats based on the map */
  804. for (n = 0, j = 0; j < map->n_pixel_formats; j++) {
  805. if ((map->pixel_formats[j].layer & id) == id)
  806. formats[n++] = map->pixel_formats[j].format;
  807. }
  808. plane_type = (i == 0) ? DRM_PLANE_TYPE_PRIMARY :
  809. DRM_PLANE_TYPE_OVERLAY;
  810. /*
  811. * All the layers except smart layer supports AFBC modifiers.
  812. */
  813. plane = drmm_universal_plane_alloc(drm, struct malidp_plane, base,
  814. crtcs, &malidp_de_plane_funcs, formats, n,
  815. (id == DE_SMART) ? linear_only_modifiers :
  816. modifiers, plane_type, NULL);
  817. if (IS_ERR(plane)) {
  818. ret = PTR_ERR(plane);
  819. goto cleanup;
  820. }
  821. drm_plane_helper_add(&plane->base,
  822. &malidp_de_plane_helper_funcs);
  823. plane->hwdev = malidp->dev;
  824. plane->layer = &map->layers[i];
  825. drm_plane_create_alpha_property(&plane->base);
  826. drm_plane_create_blend_mode_property(&plane->base, blend_caps);
  827. if (id == DE_SMART) {
  828. /* Skip the features which the SMART layer doesn't have. */
  829. continue;
  830. }
  831. drm_plane_create_rotation_property(&plane->base, DRM_MODE_ROTATE_0, flags);
  832. malidp_hw_write(malidp->dev, MALIDP_ALPHA_LUT,
  833. plane->layer->base + MALIDP_LAYER_COMPOSE);
  834. /* Attach the YUV->RGB property only to video layers */
  835. if (id & (DE_VIDEO1 | DE_VIDEO2)) {
  836. /* default encoding for YUV->RGB is BT601 NARROW */
  837. enum drm_color_encoding enc = DRM_COLOR_YCBCR_BT601;
  838. enum drm_color_range range = DRM_COLOR_YCBCR_LIMITED_RANGE;
  839. ret = drm_plane_create_color_properties(&plane->base,
  840. BIT(DRM_COLOR_YCBCR_BT601) | \
  841. BIT(DRM_COLOR_YCBCR_BT709) | \
  842. BIT(DRM_COLOR_YCBCR_BT2020),
  843. BIT(DRM_COLOR_YCBCR_LIMITED_RANGE) | \
  844. BIT(DRM_COLOR_YCBCR_FULL_RANGE),
  845. enc, range);
  846. if (!ret)
  847. /* program the HW registers */
  848. malidp_de_set_color_encoding(plane, enc, range);
  849. else
  850. DRM_WARN("Failed to create video layer %d color properties\n", id);
  851. }
  852. }
  853. kfree(formats);
  854. return 0;
  855. cleanup:
  856. kfree(formats);
  857. return ret;
  858. }