malidp_planes.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 MALIDP_LAYER_COMPOSE 0x008
  36. #define MALIDP_LAYER_SIZE 0x00c
  37. #define LAYER_H_VAL(x) (((x) & 0x1fff) << 0)
  38. #define LAYER_V_VAL(x) (((x) & 0x1fff) << 16)
  39. #define MALIDP_LAYER_COMP_SIZE 0x010
  40. #define MALIDP_LAYER_OFFSET 0x014
  41. #define MALIDP550_LS_ENABLE 0x01c
  42. #define MALIDP550_LS_R1_IN_SIZE 0x020
  43. /*
  44. * This 4-entry look-up-table is used to determine the full 8-bit alpha value
  45. * for formats with 1- or 2-bit alpha channels.
  46. * We set it to give 100%/0% opacity for 1-bit formats and 100%/66%/33%/0%
  47. * opacity for 2-bit formats.
  48. */
  49. #define MALIDP_ALPHA_LUT 0xffaa5500
  50. static void malidp_de_plane_destroy(struct drm_plane *plane)
  51. {
  52. struct malidp_plane *mp = to_malidp_plane(plane);
  53. if (mp->base.fb)
  54. drm_framebuffer_put(mp->base.fb);
  55. drm_plane_helper_disable(plane);
  56. drm_plane_cleanup(plane);
  57. devm_kfree(plane->dev->dev, 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. struct drm_rect clip = { 0 };
  125. u32 src_w, src_h;
  126. int ret;
  127. if (!crtc_state)
  128. return -EINVAL;
  129. clip.x2 = crtc_state->adjusted_mode.hdisplay;
  130. clip.y2 = crtc_state->adjusted_mode.vdisplay;
  131. ret = drm_atomic_helper_check_plane_state(state, crtc_state, &clip,
  132. 0, INT_MAX, true, true);
  133. if (ret)
  134. return ret;
  135. src_w = state->src_w >> 16;
  136. src_h = state->src_h >> 16;
  137. if ((state->crtc_w == src_w) && (state->crtc_h == src_h)) {
  138. /* Scaling not necessary for this plane. */
  139. mc->scaled_planes_mask &= ~(mp->layer->id);
  140. return 0;
  141. }
  142. if (mp->layer->id & (DE_SMART | DE_GRAPHICS2))
  143. return -EINVAL;
  144. mc = to_malidp_crtc_state(crtc_state);
  145. mc->scaled_planes_mask |= mp->layer->id;
  146. /* Defer scaling requirements calculation to the crtc check. */
  147. return 0;
  148. }
  149. static int malidp_de_plane_check(struct drm_plane *plane,
  150. struct drm_plane_state *state)
  151. {
  152. struct malidp_plane *mp = to_malidp_plane(plane);
  153. struct malidp_plane_state *ms = to_malidp_plane_state(state);
  154. struct drm_framebuffer *fb;
  155. int i, ret;
  156. if (!state->crtc || !state->fb)
  157. return 0;
  158. fb = state->fb;
  159. ms->format = malidp_hw_get_format_id(&mp->hwdev->hw->map,
  160. mp->layer->id,
  161. fb->format->format);
  162. if (ms->format == MALIDP_INVALID_FORMAT_ID)
  163. return -EINVAL;
  164. ms->n_planes = fb->format->num_planes;
  165. for (i = 0; i < ms->n_planes; i++) {
  166. if (!malidp_hw_pitch_valid(mp->hwdev, fb->pitches[i])) {
  167. DRM_DEBUG_KMS("Invalid pitch %u for plane %d\n",
  168. fb->pitches[i], i);
  169. return -EINVAL;
  170. }
  171. }
  172. if ((state->crtc_w > mp->hwdev->max_line_size) ||
  173. (state->crtc_h > mp->hwdev->max_line_size) ||
  174. (state->crtc_w < mp->hwdev->min_line_size) ||
  175. (state->crtc_h < mp->hwdev->min_line_size))
  176. return -EINVAL;
  177. /*
  178. * DP550/650 video layers can accept 3 plane formats only if
  179. * fb->pitches[1] == fb->pitches[2] since they don't have a
  180. * third plane stride register.
  181. */
  182. if (ms->n_planes == 3 &&
  183. !(mp->hwdev->hw->features & MALIDP_DEVICE_LV_HAS_3_STRIDES) &&
  184. (state->fb->pitches[1] != state->fb->pitches[2]))
  185. return -EINVAL;
  186. ret = malidp_se_check_scaling(mp, state);
  187. if (ret)
  188. return ret;
  189. /* packed RGB888 / BGR888 can't be rotated or flipped */
  190. if (state->rotation != DRM_MODE_ROTATE_0 &&
  191. (fb->format->format == DRM_FORMAT_RGB888 ||
  192. fb->format->format == DRM_FORMAT_BGR888))
  193. return -EINVAL;
  194. ms->rotmem_size = 0;
  195. if (state->rotation & MALIDP_ROTATED_MASK) {
  196. int val;
  197. val = mp->hwdev->hw->rotmem_required(mp->hwdev, state->crtc_h,
  198. state->crtc_w,
  199. fb->format->format);
  200. if (val < 0)
  201. return val;
  202. ms->rotmem_size = val;
  203. }
  204. return 0;
  205. }
  206. static void malidp_de_set_plane_pitches(struct malidp_plane *mp,
  207. int num_planes, unsigned int pitches[3])
  208. {
  209. int i;
  210. int num_strides = num_planes;
  211. if (!mp->layer->stride_offset)
  212. return;
  213. if (num_planes == 3)
  214. num_strides = (mp->hwdev->hw->features &
  215. MALIDP_DEVICE_LV_HAS_3_STRIDES) ? 3 : 2;
  216. for (i = 0; i < num_strides; ++i)
  217. malidp_hw_write(mp->hwdev, pitches[i],
  218. mp->layer->base +
  219. mp->layer->stride_offset + i * 4);
  220. }
  221. static void malidp_de_plane_update(struct drm_plane *plane,
  222. struct drm_plane_state *old_state)
  223. {
  224. struct malidp_plane *mp;
  225. struct malidp_plane_state *ms = to_malidp_plane_state(plane->state);
  226. u32 src_w, src_h, dest_w, dest_h, val;
  227. int i;
  228. mp = to_malidp_plane(plane);
  229. /* convert src values from Q16 fixed point to integer */
  230. src_w = plane->state->src_w >> 16;
  231. src_h = plane->state->src_h >> 16;
  232. dest_w = plane->state->crtc_w;
  233. dest_h = plane->state->crtc_h;
  234. malidp_hw_write(mp->hwdev, ms->format, mp->layer->base);
  235. for (i = 0; i < ms->n_planes; i++) {
  236. /* calculate the offset for the layer's plane registers */
  237. u16 ptr = mp->layer->ptr + (i << 4);
  238. dma_addr_t fb_addr = drm_fb_cma_get_gem_addr(plane->state->fb,
  239. plane->state, i);
  240. malidp_hw_write(mp->hwdev, lower_32_bits(fb_addr), ptr);
  241. malidp_hw_write(mp->hwdev, upper_32_bits(fb_addr), ptr + 4);
  242. }
  243. malidp_de_set_plane_pitches(mp, ms->n_planes,
  244. plane->state->fb->pitches);
  245. malidp_hw_write(mp->hwdev, LAYER_H_VAL(src_w) | LAYER_V_VAL(src_h),
  246. mp->layer->base + MALIDP_LAYER_SIZE);
  247. malidp_hw_write(mp->hwdev, LAYER_H_VAL(dest_w) | LAYER_V_VAL(dest_h),
  248. mp->layer->base + MALIDP_LAYER_COMP_SIZE);
  249. malidp_hw_write(mp->hwdev, LAYER_H_VAL(plane->state->crtc_x) |
  250. LAYER_V_VAL(plane->state->crtc_y),
  251. mp->layer->base + MALIDP_LAYER_OFFSET);
  252. if (mp->layer->id == DE_SMART)
  253. malidp_hw_write(mp->hwdev,
  254. LAYER_H_VAL(src_w) | LAYER_V_VAL(src_h),
  255. mp->layer->base + MALIDP550_LS_R1_IN_SIZE);
  256. /* first clear the rotation bits */
  257. val = malidp_hw_read(mp->hwdev, mp->layer->base + MALIDP_LAYER_CONTROL);
  258. val &= ~LAYER_ROT_MASK;
  259. /* setup the rotation and axis flip bits */
  260. if (plane->state->rotation & DRM_MODE_ROTATE_MASK)
  261. val |= ilog2(plane->state->rotation & DRM_MODE_ROTATE_MASK) <<
  262. LAYER_ROT_OFFSET;
  263. if (plane->state->rotation & DRM_MODE_REFLECT_X)
  264. val |= LAYER_H_FLIP;
  265. if (plane->state->rotation & DRM_MODE_REFLECT_Y)
  266. val |= LAYER_V_FLIP;
  267. /*
  268. * always enable pixel alpha blending until we have a way to change
  269. * blend modes
  270. */
  271. val &= ~LAYER_COMP_MASK;
  272. val |= LAYER_COMP_PIXEL;
  273. val &= ~LAYER_FLOWCFG(LAYER_FLOWCFG_MASK);
  274. if (plane->state->crtc) {
  275. struct malidp_crtc_state *m =
  276. to_malidp_crtc_state(plane->state->crtc->state);
  277. if (m->scaler_config.scale_enable &&
  278. m->scaler_config.plane_src_id == mp->layer->id)
  279. val |= LAYER_FLOWCFG(LAYER_FLOWCFG_SCALE_SE);
  280. }
  281. /* set the 'enable layer' bit */
  282. val |= LAYER_ENABLE;
  283. malidp_hw_write(mp->hwdev, val,
  284. mp->layer->base + MALIDP_LAYER_CONTROL);
  285. }
  286. static void malidp_de_plane_disable(struct drm_plane *plane,
  287. struct drm_plane_state *state)
  288. {
  289. struct malidp_plane *mp = to_malidp_plane(plane);
  290. malidp_hw_clearbits(mp->hwdev,
  291. LAYER_ENABLE | LAYER_FLOWCFG(LAYER_FLOWCFG_MASK),
  292. mp->layer->base + MALIDP_LAYER_CONTROL);
  293. }
  294. static const struct drm_plane_helper_funcs malidp_de_plane_helper_funcs = {
  295. .atomic_check = malidp_de_plane_check,
  296. .atomic_update = malidp_de_plane_update,
  297. .atomic_disable = malidp_de_plane_disable,
  298. };
  299. int malidp_de_planes_init(struct drm_device *drm)
  300. {
  301. struct malidp_drm *malidp = drm->dev_private;
  302. const struct malidp_hw_regmap *map = &malidp->dev->hw->map;
  303. struct malidp_plane *plane = NULL;
  304. enum drm_plane_type plane_type;
  305. unsigned long crtcs = 1 << drm->mode_config.num_crtc;
  306. unsigned long flags = DRM_MODE_ROTATE_0 | DRM_MODE_ROTATE_90 | DRM_MODE_ROTATE_180 |
  307. DRM_MODE_ROTATE_270 | DRM_MODE_REFLECT_X | DRM_MODE_REFLECT_Y;
  308. u32 *formats;
  309. int ret, i, j, n;
  310. formats = kcalloc(map->n_pixel_formats, sizeof(*formats), GFP_KERNEL);
  311. if (!formats) {
  312. ret = -ENOMEM;
  313. goto cleanup;
  314. }
  315. for (i = 0; i < map->n_layers; i++) {
  316. u8 id = map->layers[i].id;
  317. plane = kzalloc(sizeof(*plane), GFP_KERNEL);
  318. if (!plane) {
  319. ret = -ENOMEM;
  320. goto cleanup;
  321. }
  322. /* build the list of DRM supported formats based on the map */
  323. for (n = 0, j = 0; j < map->n_pixel_formats; j++) {
  324. if ((map->pixel_formats[j].layer & id) == id)
  325. formats[n++] = map->pixel_formats[j].format;
  326. }
  327. plane_type = (i == 0) ? DRM_PLANE_TYPE_PRIMARY :
  328. DRM_PLANE_TYPE_OVERLAY;
  329. ret = drm_universal_plane_init(drm, &plane->base, crtcs,
  330. &malidp_de_plane_funcs, formats,
  331. n, NULL, plane_type, NULL);
  332. if (ret < 0)
  333. goto cleanup;
  334. drm_plane_helper_add(&plane->base,
  335. &malidp_de_plane_helper_funcs);
  336. plane->hwdev = malidp->dev;
  337. plane->layer = &map->layers[i];
  338. if (id == DE_SMART) {
  339. /*
  340. * Enable the first rectangle in the SMART layer to be
  341. * able to use it as a drm plane.
  342. */
  343. malidp_hw_write(malidp->dev, 1,
  344. plane->layer->base + MALIDP550_LS_ENABLE);
  345. /* Skip the features which the SMART layer doesn't have. */
  346. continue;
  347. }
  348. drm_plane_create_rotation_property(&plane->base, DRM_MODE_ROTATE_0, flags);
  349. malidp_hw_write(malidp->dev, MALIDP_ALPHA_LUT,
  350. plane->layer->base + MALIDP_LAYER_COMPOSE);
  351. }
  352. kfree(formats);
  353. return 0;
  354. cleanup:
  355. malidp_de_planes_destroy(drm);
  356. kfree(formats);
  357. return ret;
  358. }
  359. void malidp_de_planes_destroy(struct drm_device *drm)
  360. {
  361. struct drm_plane *p, *pt;
  362. list_for_each_entry_safe(p, pt, &drm->mode_config.plane_list, head) {
  363. drm_plane_cleanup(p);
  364. kfree(p);
  365. }
  366. }