malidp_planes.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 "malidp_hw.h"
  19. #include "malidp_drv.h"
  20. /* Layer specific register offsets */
  21. #define MALIDP_LAYER_FORMAT 0x000
  22. #define MALIDP_LAYER_CONTROL 0x004
  23. #define LAYER_ENABLE (1 << 0)
  24. #define LAYER_ROT_OFFSET 8
  25. #define LAYER_H_FLIP (1 << 10)
  26. #define LAYER_V_FLIP (1 << 11)
  27. #define LAYER_ROT_MASK (0xf << 8)
  28. #define LAYER_COMP_MASK (0x3 << 12)
  29. #define LAYER_COMP_PIXEL (0x3 << 12)
  30. #define LAYER_COMP_PLANE (0x2 << 12)
  31. #define MALIDP_LAYER_COMPOSE 0x008
  32. #define MALIDP_LAYER_SIZE 0x00c
  33. #define LAYER_H_VAL(x) (((x) & 0x1fff) << 0)
  34. #define LAYER_V_VAL(x) (((x) & 0x1fff) << 16)
  35. #define MALIDP_LAYER_COMP_SIZE 0x010
  36. #define MALIDP_LAYER_OFFSET 0x014
  37. #define MALIDP550_LS_ENABLE 0x01c
  38. #define MALIDP550_LS_R1_IN_SIZE 0x020
  39. /*
  40. * This 4-entry look-up-table is used to determine the full 8-bit alpha value
  41. * for formats with 1- or 2-bit alpha channels.
  42. * We set it to give 100%/0% opacity for 1-bit formats and 100%/66%/33%/0%
  43. * opacity for 2-bit formats.
  44. */
  45. #define MALIDP_ALPHA_LUT 0xffaa5500
  46. static void malidp_de_plane_destroy(struct drm_plane *plane)
  47. {
  48. struct malidp_plane *mp = to_malidp_plane(plane);
  49. if (mp->base.fb)
  50. drm_framebuffer_unreference(mp->base.fb);
  51. drm_plane_helper_disable(plane);
  52. drm_plane_cleanup(plane);
  53. devm_kfree(plane->dev->dev, mp);
  54. }
  55. static struct
  56. drm_plane_state *malidp_duplicate_plane_state(struct drm_plane *plane)
  57. {
  58. struct malidp_plane_state *state, *m_state;
  59. if (!plane->state)
  60. return NULL;
  61. state = kmalloc(sizeof(*state), GFP_KERNEL);
  62. if (!state)
  63. return NULL;
  64. m_state = to_malidp_plane_state(plane->state);
  65. __drm_atomic_helper_plane_duplicate_state(plane, &state->base);
  66. state->rotmem_size = m_state->rotmem_size;
  67. state->format = m_state->format;
  68. state->n_planes = m_state->n_planes;
  69. return &state->base;
  70. }
  71. static void malidp_destroy_plane_state(struct drm_plane *plane,
  72. struct drm_plane_state *state)
  73. {
  74. struct malidp_plane_state *m_state = to_malidp_plane_state(state);
  75. __drm_atomic_helper_plane_destroy_state(state);
  76. kfree(m_state);
  77. }
  78. static const struct drm_plane_funcs malidp_de_plane_funcs = {
  79. .update_plane = drm_atomic_helper_update_plane,
  80. .disable_plane = drm_atomic_helper_disable_plane,
  81. .set_property = drm_atomic_helper_plane_set_property,
  82. .destroy = malidp_de_plane_destroy,
  83. .reset = drm_atomic_helper_plane_reset,
  84. .atomic_duplicate_state = malidp_duplicate_plane_state,
  85. .atomic_destroy_state = malidp_destroy_plane_state,
  86. };
  87. static int malidp_de_plane_check(struct drm_plane *plane,
  88. struct drm_plane_state *state)
  89. {
  90. struct malidp_plane *mp = to_malidp_plane(plane);
  91. struct malidp_plane_state *ms = to_malidp_plane_state(state);
  92. struct drm_crtc_state *crtc_state;
  93. struct drm_framebuffer *fb;
  94. struct drm_rect clip = { 0 };
  95. int i, ret;
  96. u32 src_w, src_h;
  97. if (!state->crtc || !state->fb)
  98. return 0;
  99. fb = state->fb;
  100. ms->format = malidp_hw_get_format_id(&mp->hwdev->map, mp->layer->id,
  101. fb->format->format);
  102. if (ms->format == MALIDP_INVALID_FORMAT_ID)
  103. return -EINVAL;
  104. ms->n_planes = fb->format->num_planes;
  105. for (i = 0; i < ms->n_planes; i++) {
  106. if (!malidp_hw_pitch_valid(mp->hwdev, fb->pitches[i])) {
  107. DRM_DEBUG_KMS("Invalid pitch %u for plane %d\n",
  108. fb->pitches[i], i);
  109. return -EINVAL;
  110. }
  111. }
  112. src_w = state->src_w >> 16;
  113. src_h = state->src_h >> 16;
  114. if ((state->crtc_w > mp->hwdev->max_line_size) ||
  115. (state->crtc_h > mp->hwdev->max_line_size) ||
  116. (state->crtc_w < mp->hwdev->min_line_size) ||
  117. (state->crtc_h < mp->hwdev->min_line_size))
  118. return -EINVAL;
  119. /*
  120. * DP550/650 video layers can accept 3 plane formats only if
  121. * fb->pitches[1] == fb->pitches[2] since they don't have a
  122. * third plane stride register.
  123. */
  124. if (ms->n_planes == 3 &&
  125. !(mp->hwdev->features & MALIDP_DEVICE_LV_HAS_3_STRIDES) &&
  126. (state->fb->pitches[1] != state->fb->pitches[2]))
  127. return -EINVAL;
  128. /* packed RGB888 / BGR888 can't be rotated or flipped */
  129. if (state->rotation != DRM_ROTATE_0 &&
  130. (fb->format->format == DRM_FORMAT_RGB888 ||
  131. fb->format->format == DRM_FORMAT_BGR888))
  132. return -EINVAL;
  133. crtc_state = drm_atomic_get_existing_crtc_state(state->state, state->crtc);
  134. clip.x2 = crtc_state->adjusted_mode.hdisplay;
  135. clip.y2 = crtc_state->adjusted_mode.vdisplay;
  136. ret = drm_plane_helper_check_state(state, &clip,
  137. DRM_PLANE_HELPER_NO_SCALING,
  138. DRM_PLANE_HELPER_NO_SCALING,
  139. true, true);
  140. if (ret)
  141. return ret;
  142. ms->rotmem_size = 0;
  143. if (state->rotation & MALIDP_ROTATED_MASK) {
  144. int val;
  145. val = mp->hwdev->rotmem_required(mp->hwdev, state->crtc_h,
  146. state->crtc_w,
  147. fb->format->format);
  148. if (val < 0)
  149. return val;
  150. ms->rotmem_size = val;
  151. }
  152. return 0;
  153. }
  154. static void malidp_de_set_plane_pitches(struct malidp_plane *mp,
  155. int num_planes, unsigned int pitches[3])
  156. {
  157. int i;
  158. int num_strides = num_planes;
  159. if (!mp->layer->stride_offset)
  160. return;
  161. if (num_planes == 3)
  162. num_strides = (mp->hwdev->features &
  163. MALIDP_DEVICE_LV_HAS_3_STRIDES) ? 3 : 2;
  164. for (i = 0; i < num_strides; ++i)
  165. malidp_hw_write(mp->hwdev, pitches[i],
  166. mp->layer->base +
  167. mp->layer->stride_offset + i * 4);
  168. }
  169. static void malidp_de_plane_update(struct drm_plane *plane,
  170. struct drm_plane_state *old_state)
  171. {
  172. struct drm_gem_cma_object *obj;
  173. struct malidp_plane *mp;
  174. const struct malidp_hw_regmap *map;
  175. struct malidp_plane_state *ms = to_malidp_plane_state(plane->state);
  176. u16 ptr;
  177. u32 src_w, src_h, dest_w, dest_h, val;
  178. int i;
  179. mp = to_malidp_plane(plane);
  180. map = &mp->hwdev->map;
  181. /* convert src values from Q16 fixed point to integer */
  182. src_w = plane->state->src_w >> 16;
  183. src_h = plane->state->src_h >> 16;
  184. dest_w = plane->state->crtc_w;
  185. dest_h = plane->state->crtc_h;
  186. malidp_hw_write(mp->hwdev, ms->format, mp->layer->base);
  187. for (i = 0; i < ms->n_planes; i++) {
  188. /* calculate the offset for the layer's plane registers */
  189. ptr = mp->layer->ptr + (i << 4);
  190. obj = drm_fb_cma_get_gem_obj(plane->state->fb, i);
  191. obj->paddr += plane->state->fb->offsets[i];
  192. malidp_hw_write(mp->hwdev, lower_32_bits(obj->paddr), ptr);
  193. malidp_hw_write(mp->hwdev, upper_32_bits(obj->paddr), ptr + 4);
  194. }
  195. malidp_de_set_plane_pitches(mp, ms->n_planes,
  196. plane->state->fb->pitches);
  197. malidp_hw_write(mp->hwdev, LAYER_H_VAL(src_w) | LAYER_V_VAL(src_h),
  198. mp->layer->base + MALIDP_LAYER_SIZE);
  199. malidp_hw_write(mp->hwdev, LAYER_H_VAL(dest_w) | LAYER_V_VAL(dest_h),
  200. mp->layer->base + MALIDP_LAYER_COMP_SIZE);
  201. malidp_hw_write(mp->hwdev, LAYER_H_VAL(plane->state->crtc_x) |
  202. LAYER_V_VAL(plane->state->crtc_y),
  203. mp->layer->base + MALIDP_LAYER_OFFSET);
  204. if (mp->layer->id == DE_SMART)
  205. malidp_hw_write(mp->hwdev,
  206. LAYER_H_VAL(src_w) | LAYER_V_VAL(src_h),
  207. mp->layer->base + MALIDP550_LS_R1_IN_SIZE);
  208. /* first clear the rotation bits */
  209. val = malidp_hw_read(mp->hwdev, mp->layer->base + MALIDP_LAYER_CONTROL);
  210. val &= ~LAYER_ROT_MASK;
  211. /* setup the rotation and axis flip bits */
  212. if (plane->state->rotation & DRM_ROTATE_MASK)
  213. val |= ilog2(plane->state->rotation & DRM_ROTATE_MASK) <<
  214. LAYER_ROT_OFFSET;
  215. if (plane->state->rotation & DRM_REFLECT_X)
  216. val |= LAYER_H_FLIP;
  217. if (plane->state->rotation & DRM_REFLECT_Y)
  218. val |= LAYER_V_FLIP;
  219. /*
  220. * always enable pixel alpha blending until we have a way to change
  221. * blend modes
  222. */
  223. val &= ~LAYER_COMP_MASK;
  224. val |= LAYER_COMP_PIXEL;
  225. /* set the 'enable layer' bit */
  226. val |= LAYER_ENABLE;
  227. malidp_hw_write(mp->hwdev, val,
  228. mp->layer->base + MALIDP_LAYER_CONTROL);
  229. }
  230. static void malidp_de_plane_disable(struct drm_plane *plane,
  231. struct drm_plane_state *state)
  232. {
  233. struct malidp_plane *mp = to_malidp_plane(plane);
  234. malidp_hw_clearbits(mp->hwdev, LAYER_ENABLE,
  235. mp->layer->base + MALIDP_LAYER_CONTROL);
  236. }
  237. static const struct drm_plane_helper_funcs malidp_de_plane_helper_funcs = {
  238. .atomic_check = malidp_de_plane_check,
  239. .atomic_update = malidp_de_plane_update,
  240. .atomic_disable = malidp_de_plane_disable,
  241. };
  242. int malidp_de_planes_init(struct drm_device *drm)
  243. {
  244. struct malidp_drm *malidp = drm->dev_private;
  245. const struct malidp_hw_regmap *map = &malidp->dev->map;
  246. struct malidp_plane *plane = NULL;
  247. enum drm_plane_type plane_type;
  248. unsigned long crtcs = 1 << drm->mode_config.num_crtc;
  249. unsigned long flags = DRM_ROTATE_0 | DRM_ROTATE_90 | DRM_ROTATE_180 |
  250. DRM_ROTATE_270 | DRM_REFLECT_X | DRM_REFLECT_Y;
  251. u32 *formats;
  252. int ret, i, j, n;
  253. formats = kcalloc(map->n_pixel_formats, sizeof(*formats), GFP_KERNEL);
  254. if (!formats) {
  255. ret = -ENOMEM;
  256. goto cleanup;
  257. }
  258. for (i = 0; i < map->n_layers; i++) {
  259. u8 id = map->layers[i].id;
  260. plane = kzalloc(sizeof(*plane), GFP_KERNEL);
  261. if (!plane) {
  262. ret = -ENOMEM;
  263. goto cleanup;
  264. }
  265. /* build the list of DRM supported formats based on the map */
  266. for (n = 0, j = 0; j < map->n_pixel_formats; j++) {
  267. if ((map->pixel_formats[j].layer & id) == id)
  268. formats[n++] = map->pixel_formats[j].format;
  269. }
  270. plane_type = (i == 0) ? DRM_PLANE_TYPE_PRIMARY :
  271. DRM_PLANE_TYPE_OVERLAY;
  272. ret = drm_universal_plane_init(drm, &plane->base, crtcs,
  273. &malidp_de_plane_funcs, formats,
  274. n, plane_type, NULL);
  275. if (ret < 0)
  276. goto cleanup;
  277. drm_plane_helper_add(&plane->base,
  278. &malidp_de_plane_helper_funcs);
  279. plane->hwdev = malidp->dev;
  280. plane->layer = &map->layers[i];
  281. if (id == DE_SMART) {
  282. /*
  283. * Enable the first rectangle in the SMART layer to be
  284. * able to use it as a drm plane.
  285. */
  286. malidp_hw_write(malidp->dev, 1,
  287. plane->layer->base + MALIDP550_LS_ENABLE);
  288. /* Skip the features which the SMART layer doesn't have. */
  289. continue;
  290. }
  291. drm_plane_create_rotation_property(&plane->base, DRM_ROTATE_0, flags);
  292. malidp_hw_write(malidp->dev, MALIDP_ALPHA_LUT,
  293. plane->layer->base + MALIDP_LAYER_COMPOSE);
  294. }
  295. kfree(formats);
  296. return 0;
  297. cleanup:
  298. malidp_de_planes_destroy(drm);
  299. kfree(formats);
  300. return ret;
  301. }
  302. void malidp_de_planes_destroy(struct drm_device *drm)
  303. {
  304. struct drm_plane *p, *pt;
  305. list_for_each_entry_safe(p, pt, &drm->mode_config.plane_list, head) {
  306. drm_plane_cleanup(p);
  307. kfree(p);
  308. }
  309. }