malidp_planes.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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_unreference(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_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. .set_property = drm_atomic_helper_plane_set_property,
  113. .destroy = malidp_de_plane_destroy,
  114. .reset = malidp_plane_reset,
  115. .atomic_duplicate_state = malidp_duplicate_plane_state,
  116. .atomic_destroy_state = malidp_destroy_plane_state,
  117. .atomic_print_state = malidp_plane_atomic_print_state,
  118. };
  119. static int malidp_se_check_scaling(struct malidp_plane *mp,
  120. struct drm_plane_state *state)
  121. {
  122. struct drm_crtc_state *crtc_state =
  123. drm_atomic_get_existing_crtc_state(state->state, state->crtc);
  124. struct malidp_crtc_state *mc;
  125. struct drm_rect clip = { 0 };
  126. u32 src_w, src_h;
  127. int ret;
  128. if (!crtc_state)
  129. return -EINVAL;
  130. clip.x2 = crtc_state->adjusted_mode.hdisplay;
  131. clip.y2 = crtc_state->adjusted_mode.vdisplay;
  132. ret = drm_plane_helper_check_state(state, &clip, 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->map, mp->layer->id,
  160. fb->format->format);
  161. if (ms->format == MALIDP_INVALID_FORMAT_ID)
  162. return -EINVAL;
  163. ms->n_planes = fb->format->num_planes;
  164. for (i = 0; i < ms->n_planes; i++) {
  165. if (!malidp_hw_pitch_valid(mp->hwdev, fb->pitches[i])) {
  166. DRM_DEBUG_KMS("Invalid pitch %u for plane %d\n",
  167. fb->pitches[i], i);
  168. return -EINVAL;
  169. }
  170. }
  171. if ((state->crtc_w > mp->hwdev->max_line_size) ||
  172. (state->crtc_h > mp->hwdev->max_line_size) ||
  173. (state->crtc_w < mp->hwdev->min_line_size) ||
  174. (state->crtc_h < mp->hwdev->min_line_size))
  175. return -EINVAL;
  176. /*
  177. * DP550/650 video layers can accept 3 plane formats only if
  178. * fb->pitches[1] == fb->pitches[2] since they don't have a
  179. * third plane stride register.
  180. */
  181. if (ms->n_planes == 3 &&
  182. !(mp->hwdev->features & MALIDP_DEVICE_LV_HAS_3_STRIDES) &&
  183. (state->fb->pitches[1] != state->fb->pitches[2]))
  184. return -EINVAL;
  185. ret = malidp_se_check_scaling(mp, state);
  186. if (ret)
  187. return ret;
  188. /* packed RGB888 / BGR888 can't be rotated or flipped */
  189. if (state->rotation != DRM_ROTATE_0 &&
  190. (fb->format->format == DRM_FORMAT_RGB888 ||
  191. fb->format->format == DRM_FORMAT_BGR888))
  192. return -EINVAL;
  193. ms->rotmem_size = 0;
  194. if (state->rotation & MALIDP_ROTATED_MASK) {
  195. int val;
  196. val = mp->hwdev->rotmem_required(mp->hwdev, state->crtc_h,
  197. state->crtc_w,
  198. fb->format->format);
  199. if (val < 0)
  200. return val;
  201. ms->rotmem_size = val;
  202. }
  203. return 0;
  204. }
  205. static void malidp_de_set_plane_pitches(struct malidp_plane *mp,
  206. int num_planes, unsigned int pitches[3])
  207. {
  208. int i;
  209. int num_strides = num_planes;
  210. if (!mp->layer->stride_offset)
  211. return;
  212. if (num_planes == 3)
  213. num_strides = (mp->hwdev->features &
  214. MALIDP_DEVICE_LV_HAS_3_STRIDES) ? 3 : 2;
  215. for (i = 0; i < num_strides; ++i)
  216. malidp_hw_write(mp->hwdev, pitches[i],
  217. mp->layer->base +
  218. mp->layer->stride_offset + i * 4);
  219. }
  220. static void malidp_de_plane_update(struct drm_plane *plane,
  221. struct drm_plane_state *old_state)
  222. {
  223. struct drm_gem_cma_object *obj;
  224. struct malidp_plane *mp;
  225. const struct malidp_hw_regmap *map;
  226. struct malidp_plane_state *ms = to_malidp_plane_state(plane->state);
  227. u16 ptr;
  228. u32 src_w, src_h, dest_w, dest_h, val;
  229. int i;
  230. mp = to_malidp_plane(plane);
  231. map = &mp->hwdev->map;
  232. /* convert src values from Q16 fixed point to integer */
  233. src_w = plane->state->src_w >> 16;
  234. src_h = plane->state->src_h >> 16;
  235. dest_w = plane->state->crtc_w;
  236. dest_h = plane->state->crtc_h;
  237. malidp_hw_write(mp->hwdev, ms->format, mp->layer->base);
  238. for (i = 0; i < ms->n_planes; i++) {
  239. /* calculate the offset for the layer's plane registers */
  240. ptr = mp->layer->ptr + (i << 4);
  241. obj = drm_fb_cma_get_gem_obj(plane->state->fb, i);
  242. obj->paddr += plane->state->fb->offsets[i];
  243. malidp_hw_write(mp->hwdev, lower_32_bits(obj->paddr), ptr);
  244. malidp_hw_write(mp->hwdev, upper_32_bits(obj->paddr), ptr + 4);
  245. }
  246. malidp_de_set_plane_pitches(mp, ms->n_planes,
  247. plane->state->fb->pitches);
  248. malidp_hw_write(mp->hwdev, LAYER_H_VAL(src_w) | LAYER_V_VAL(src_h),
  249. mp->layer->base + MALIDP_LAYER_SIZE);
  250. malidp_hw_write(mp->hwdev, LAYER_H_VAL(dest_w) | LAYER_V_VAL(dest_h),
  251. mp->layer->base + MALIDP_LAYER_COMP_SIZE);
  252. malidp_hw_write(mp->hwdev, LAYER_H_VAL(plane->state->crtc_x) |
  253. LAYER_V_VAL(plane->state->crtc_y),
  254. mp->layer->base + MALIDP_LAYER_OFFSET);
  255. if (mp->layer->id == DE_SMART)
  256. malidp_hw_write(mp->hwdev,
  257. LAYER_H_VAL(src_w) | LAYER_V_VAL(src_h),
  258. mp->layer->base + MALIDP550_LS_R1_IN_SIZE);
  259. /* first clear the rotation bits */
  260. val = malidp_hw_read(mp->hwdev, mp->layer->base + MALIDP_LAYER_CONTROL);
  261. val &= ~LAYER_ROT_MASK;
  262. /* setup the rotation and axis flip bits */
  263. if (plane->state->rotation & DRM_ROTATE_MASK)
  264. val |= ilog2(plane->state->rotation & DRM_ROTATE_MASK) <<
  265. LAYER_ROT_OFFSET;
  266. if (plane->state->rotation & DRM_REFLECT_X)
  267. val |= LAYER_H_FLIP;
  268. if (plane->state->rotation & DRM_REFLECT_Y)
  269. val |= LAYER_V_FLIP;
  270. /*
  271. * always enable pixel alpha blending until we have a way to change
  272. * blend modes
  273. */
  274. val &= ~LAYER_COMP_MASK;
  275. val |= LAYER_COMP_PIXEL;
  276. val &= ~LAYER_FLOWCFG(LAYER_FLOWCFG_MASK);
  277. if (plane->state->crtc) {
  278. struct malidp_crtc_state *m =
  279. to_malidp_crtc_state(plane->state->crtc->state);
  280. if (m->scaler_config.scale_enable &&
  281. m->scaler_config.plane_src_id == mp->layer->id)
  282. val |= LAYER_FLOWCFG(LAYER_FLOWCFG_SCALE_SE);
  283. }
  284. /* set the 'enable layer' bit */
  285. val |= LAYER_ENABLE;
  286. malidp_hw_write(mp->hwdev, val,
  287. mp->layer->base + MALIDP_LAYER_CONTROL);
  288. }
  289. static void malidp_de_plane_disable(struct drm_plane *plane,
  290. struct drm_plane_state *state)
  291. {
  292. struct malidp_plane *mp = to_malidp_plane(plane);
  293. malidp_hw_clearbits(mp->hwdev,
  294. LAYER_ENABLE | LAYER_FLOWCFG(LAYER_FLOWCFG_MASK),
  295. mp->layer->base + MALIDP_LAYER_CONTROL);
  296. }
  297. static const struct drm_plane_helper_funcs malidp_de_plane_helper_funcs = {
  298. .atomic_check = malidp_de_plane_check,
  299. .atomic_update = malidp_de_plane_update,
  300. .atomic_disable = malidp_de_plane_disable,
  301. };
  302. int malidp_de_planes_init(struct drm_device *drm)
  303. {
  304. struct malidp_drm *malidp = drm->dev_private;
  305. const struct malidp_hw_regmap *map = &malidp->dev->map;
  306. struct malidp_plane *plane = NULL;
  307. enum drm_plane_type plane_type;
  308. unsigned long crtcs = 1 << drm->mode_config.num_crtc;
  309. unsigned long flags = DRM_ROTATE_0 | DRM_ROTATE_90 | DRM_ROTATE_180 |
  310. DRM_ROTATE_270 | DRM_REFLECT_X | DRM_REFLECT_Y;
  311. u32 *formats;
  312. int ret, i, j, n;
  313. formats = kcalloc(map->n_pixel_formats, sizeof(*formats), GFP_KERNEL);
  314. if (!formats) {
  315. ret = -ENOMEM;
  316. goto cleanup;
  317. }
  318. for (i = 0; i < map->n_layers; i++) {
  319. u8 id = map->layers[i].id;
  320. plane = kzalloc(sizeof(*plane), GFP_KERNEL);
  321. if (!plane) {
  322. ret = -ENOMEM;
  323. goto cleanup;
  324. }
  325. /* build the list of DRM supported formats based on the map */
  326. for (n = 0, j = 0; j < map->n_pixel_formats; j++) {
  327. if ((map->pixel_formats[j].layer & id) == id)
  328. formats[n++] = map->pixel_formats[j].format;
  329. }
  330. plane_type = (i == 0) ? DRM_PLANE_TYPE_PRIMARY :
  331. DRM_PLANE_TYPE_OVERLAY;
  332. ret = drm_universal_plane_init(drm, &plane->base, crtcs,
  333. &malidp_de_plane_funcs, formats,
  334. n, plane_type, NULL);
  335. if (ret < 0)
  336. goto cleanup;
  337. drm_plane_helper_add(&plane->base,
  338. &malidp_de_plane_helper_funcs);
  339. plane->hwdev = malidp->dev;
  340. plane->layer = &map->layers[i];
  341. if (id == DE_SMART) {
  342. /*
  343. * Enable the first rectangle in the SMART layer to be
  344. * able to use it as a drm plane.
  345. */
  346. malidp_hw_write(malidp->dev, 1,
  347. plane->layer->base + MALIDP550_LS_ENABLE);
  348. /* Skip the features which the SMART layer doesn't have. */
  349. continue;
  350. }
  351. drm_plane_create_rotation_property(&plane->base, DRM_ROTATE_0, flags);
  352. malidp_hw_write(malidp->dev, MALIDP_ALPHA_LUT,
  353. plane->layer->base + MALIDP_LAYER_COMPOSE);
  354. }
  355. kfree(formats);
  356. return 0;
  357. cleanup:
  358. malidp_de_planes_destroy(drm);
  359. kfree(formats);
  360. return ret;
  361. }
  362. void malidp_de_planes_destroy(struct drm_device *drm)
  363. {
  364. struct drm_plane *p, *pt;
  365. list_for_each_entry_safe(p, pt, &drm->mode_config.plane_list, head) {
  366. drm_plane_cleanup(p);
  367. kfree(p);
  368. }
  369. }