atmel_hlcdc_plane.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. /*
  2. * Copyright (C) 2014 Free Electrons
  3. * Copyright (C) 2014 Atmel
  4. *
  5. * Author: Boris BREZILLON <boris.brezillon@free-electrons.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "atmel_hlcdc_dc.h"
  20. /**
  21. * Atmel HLCDC Plane state structure.
  22. *
  23. * @base: DRM plane state
  24. * @crtc_x: x position of the plane relative to the CRTC
  25. * @crtc_y: y position of the plane relative to the CRTC
  26. * @crtc_w: visible width of the plane
  27. * @crtc_h: visible height of the plane
  28. * @src_x: x buffer position
  29. * @src_y: y buffer position
  30. * @src_w: buffer width
  31. * @src_h: buffer height
  32. * @alpha: alpha blending of the plane
  33. * @bpp: bytes per pixel deduced from pixel_format
  34. * @offsets: offsets to apply to the GEM buffers
  35. * @xstride: value to add to the pixel pointer between each line
  36. * @pstride: value to add to the pixel pointer between each pixel
  37. * @nplanes: number of planes (deduced from pixel_format)
  38. * @prepared: plane update has been prepared
  39. */
  40. struct atmel_hlcdc_plane_state {
  41. struct drm_plane_state base;
  42. int crtc_x;
  43. int crtc_y;
  44. unsigned int crtc_w;
  45. unsigned int crtc_h;
  46. uint32_t src_x;
  47. uint32_t src_y;
  48. uint32_t src_w;
  49. uint32_t src_h;
  50. u8 alpha;
  51. bool disc_updated;
  52. int disc_x;
  53. int disc_y;
  54. int disc_w;
  55. int disc_h;
  56. int ahb_id;
  57. /* These fields are private and should not be touched */
  58. int bpp[ATMEL_HLCDC_MAX_PLANES];
  59. unsigned int offsets[ATMEL_HLCDC_MAX_PLANES];
  60. int xstride[ATMEL_HLCDC_MAX_PLANES];
  61. int pstride[ATMEL_HLCDC_MAX_PLANES];
  62. int nplanes;
  63. bool prepared;
  64. };
  65. static inline struct atmel_hlcdc_plane_state *
  66. drm_plane_state_to_atmel_hlcdc_plane_state(struct drm_plane_state *s)
  67. {
  68. return container_of(s, struct atmel_hlcdc_plane_state, base);
  69. }
  70. #define SUBPIXEL_MASK 0xffff
  71. static uint32_t rgb_formats[] = {
  72. DRM_FORMAT_XRGB4444,
  73. DRM_FORMAT_ARGB4444,
  74. DRM_FORMAT_RGBA4444,
  75. DRM_FORMAT_ARGB1555,
  76. DRM_FORMAT_RGB565,
  77. DRM_FORMAT_RGB888,
  78. DRM_FORMAT_XRGB8888,
  79. DRM_FORMAT_ARGB8888,
  80. DRM_FORMAT_RGBA8888,
  81. };
  82. struct atmel_hlcdc_formats atmel_hlcdc_plane_rgb_formats = {
  83. .formats = rgb_formats,
  84. .nformats = ARRAY_SIZE(rgb_formats),
  85. };
  86. static uint32_t rgb_and_yuv_formats[] = {
  87. DRM_FORMAT_XRGB4444,
  88. DRM_FORMAT_ARGB4444,
  89. DRM_FORMAT_RGBA4444,
  90. DRM_FORMAT_ARGB1555,
  91. DRM_FORMAT_RGB565,
  92. DRM_FORMAT_RGB888,
  93. DRM_FORMAT_XRGB8888,
  94. DRM_FORMAT_ARGB8888,
  95. DRM_FORMAT_RGBA8888,
  96. DRM_FORMAT_AYUV,
  97. DRM_FORMAT_YUYV,
  98. DRM_FORMAT_UYVY,
  99. DRM_FORMAT_YVYU,
  100. DRM_FORMAT_VYUY,
  101. DRM_FORMAT_NV21,
  102. DRM_FORMAT_NV61,
  103. DRM_FORMAT_YUV422,
  104. DRM_FORMAT_YUV420,
  105. };
  106. struct atmel_hlcdc_formats atmel_hlcdc_plane_rgb_and_yuv_formats = {
  107. .formats = rgb_and_yuv_formats,
  108. .nformats = ARRAY_SIZE(rgb_and_yuv_formats),
  109. };
  110. static int atmel_hlcdc_format_to_plane_mode(u32 format, u32 *mode)
  111. {
  112. switch (format) {
  113. case DRM_FORMAT_XRGB4444:
  114. *mode = ATMEL_HLCDC_XRGB4444_MODE;
  115. break;
  116. case DRM_FORMAT_ARGB4444:
  117. *mode = ATMEL_HLCDC_ARGB4444_MODE;
  118. break;
  119. case DRM_FORMAT_RGBA4444:
  120. *mode = ATMEL_HLCDC_RGBA4444_MODE;
  121. break;
  122. case DRM_FORMAT_RGB565:
  123. *mode = ATMEL_HLCDC_RGB565_MODE;
  124. break;
  125. case DRM_FORMAT_RGB888:
  126. *mode = ATMEL_HLCDC_RGB888_MODE;
  127. break;
  128. case DRM_FORMAT_ARGB1555:
  129. *mode = ATMEL_HLCDC_ARGB1555_MODE;
  130. break;
  131. case DRM_FORMAT_XRGB8888:
  132. *mode = ATMEL_HLCDC_XRGB8888_MODE;
  133. break;
  134. case DRM_FORMAT_ARGB8888:
  135. *mode = ATMEL_HLCDC_ARGB8888_MODE;
  136. break;
  137. case DRM_FORMAT_RGBA8888:
  138. *mode = ATMEL_HLCDC_RGBA8888_MODE;
  139. break;
  140. case DRM_FORMAT_AYUV:
  141. *mode = ATMEL_HLCDC_AYUV_MODE;
  142. break;
  143. case DRM_FORMAT_YUYV:
  144. *mode = ATMEL_HLCDC_YUYV_MODE;
  145. break;
  146. case DRM_FORMAT_UYVY:
  147. *mode = ATMEL_HLCDC_UYVY_MODE;
  148. break;
  149. case DRM_FORMAT_YVYU:
  150. *mode = ATMEL_HLCDC_YVYU_MODE;
  151. break;
  152. case DRM_FORMAT_VYUY:
  153. *mode = ATMEL_HLCDC_VYUY_MODE;
  154. break;
  155. case DRM_FORMAT_NV21:
  156. *mode = ATMEL_HLCDC_NV21_MODE;
  157. break;
  158. case DRM_FORMAT_NV61:
  159. *mode = ATMEL_HLCDC_NV61_MODE;
  160. break;
  161. case DRM_FORMAT_YUV420:
  162. *mode = ATMEL_HLCDC_YUV420_MODE;
  163. break;
  164. case DRM_FORMAT_YUV422:
  165. *mode = ATMEL_HLCDC_YUV422_MODE;
  166. break;
  167. default:
  168. return -ENOTSUPP;
  169. }
  170. return 0;
  171. }
  172. static bool atmel_hlcdc_format_embeds_alpha(u32 format)
  173. {
  174. int i;
  175. for (i = 0; i < sizeof(format); i++) {
  176. char tmp = (format >> (8 * i)) & 0xff;
  177. if (tmp == 'A')
  178. return true;
  179. }
  180. return false;
  181. }
  182. static u32 heo_downscaling_xcoef[] = {
  183. 0x11343311,
  184. 0x000000f7,
  185. 0x1635300c,
  186. 0x000000f9,
  187. 0x1b362c08,
  188. 0x000000fb,
  189. 0x1f372804,
  190. 0x000000fe,
  191. 0x24382400,
  192. 0x00000000,
  193. 0x28371ffe,
  194. 0x00000004,
  195. 0x2c361bfb,
  196. 0x00000008,
  197. 0x303516f9,
  198. 0x0000000c,
  199. };
  200. static u32 heo_downscaling_ycoef[] = {
  201. 0x00123737,
  202. 0x00173732,
  203. 0x001b382d,
  204. 0x001f3928,
  205. 0x00243824,
  206. 0x0028391f,
  207. 0x002d381b,
  208. 0x00323717,
  209. };
  210. static u32 heo_upscaling_xcoef[] = {
  211. 0xf74949f7,
  212. 0x00000000,
  213. 0xf55f33fb,
  214. 0x000000fe,
  215. 0xf5701efe,
  216. 0x000000ff,
  217. 0xf87c0dff,
  218. 0x00000000,
  219. 0x00800000,
  220. 0x00000000,
  221. 0x0d7cf800,
  222. 0x000000ff,
  223. 0x1e70f5ff,
  224. 0x000000fe,
  225. 0x335ff5fe,
  226. 0x000000fb,
  227. };
  228. static u32 heo_upscaling_ycoef[] = {
  229. 0x00004040,
  230. 0x00075920,
  231. 0x00056f0c,
  232. 0x00027b03,
  233. 0x00008000,
  234. 0x00037b02,
  235. 0x000c6f05,
  236. 0x00205907,
  237. };
  238. static void
  239. atmel_hlcdc_plane_update_pos_and_size(struct atmel_hlcdc_plane *plane,
  240. struct atmel_hlcdc_plane_state *state)
  241. {
  242. const struct atmel_hlcdc_layer_cfg_layout *layout =
  243. &plane->layer.desc->layout;
  244. if (layout->size)
  245. atmel_hlcdc_layer_update_cfg(&plane->layer,
  246. layout->size,
  247. 0xffffffff,
  248. (state->crtc_w - 1) |
  249. ((state->crtc_h - 1) << 16));
  250. if (layout->memsize)
  251. atmel_hlcdc_layer_update_cfg(&plane->layer,
  252. layout->memsize,
  253. 0xffffffff,
  254. (state->src_w - 1) |
  255. ((state->src_h - 1) << 16));
  256. if (layout->pos)
  257. atmel_hlcdc_layer_update_cfg(&plane->layer,
  258. layout->pos,
  259. 0xffffffff,
  260. state->crtc_x |
  261. (state->crtc_y << 16));
  262. /* TODO: rework the rescaling part */
  263. if (state->crtc_w != state->src_w || state->crtc_h != state->src_h) {
  264. u32 factor_reg = 0;
  265. if (state->crtc_w != state->src_w) {
  266. int i;
  267. u32 factor;
  268. u32 *coeff_tab = heo_upscaling_xcoef;
  269. u32 max_memsize;
  270. if (state->crtc_w < state->src_w)
  271. coeff_tab = heo_downscaling_xcoef;
  272. for (i = 0; i < ARRAY_SIZE(heo_upscaling_xcoef); i++)
  273. atmel_hlcdc_layer_update_cfg(&plane->layer,
  274. 17 + i,
  275. 0xffffffff,
  276. coeff_tab[i]);
  277. factor = ((8 * 256 * state->src_w) - (256 * 4)) /
  278. state->crtc_w;
  279. factor++;
  280. max_memsize = ((factor * state->crtc_w) + (256 * 4)) /
  281. 2048;
  282. if (max_memsize > state->src_w)
  283. factor--;
  284. factor_reg |= factor | 0x80000000;
  285. }
  286. if (state->crtc_h != state->src_h) {
  287. int i;
  288. u32 factor;
  289. u32 *coeff_tab = heo_upscaling_ycoef;
  290. u32 max_memsize;
  291. if (state->crtc_w < state->src_w)
  292. coeff_tab = heo_downscaling_ycoef;
  293. for (i = 0; i < ARRAY_SIZE(heo_upscaling_ycoef); i++)
  294. atmel_hlcdc_layer_update_cfg(&plane->layer,
  295. 33 + i,
  296. 0xffffffff,
  297. coeff_tab[i]);
  298. factor = ((8 * 256 * state->src_w) - (256 * 4)) /
  299. state->crtc_w;
  300. factor++;
  301. max_memsize = ((factor * state->crtc_w) + (256 * 4)) /
  302. 2048;
  303. if (max_memsize > state->src_w)
  304. factor--;
  305. factor_reg |= (factor << 16) | 0x80000000;
  306. }
  307. atmel_hlcdc_layer_update_cfg(&plane->layer, 13, 0xffffffff,
  308. factor_reg);
  309. }
  310. }
  311. static void
  312. atmel_hlcdc_plane_update_general_settings(struct atmel_hlcdc_plane *plane,
  313. struct atmel_hlcdc_plane_state *state)
  314. {
  315. const struct atmel_hlcdc_layer_cfg_layout *layout =
  316. &plane->layer.desc->layout;
  317. unsigned int cfg = ATMEL_HLCDC_LAYER_DMA;
  318. if (plane->base.type != DRM_PLANE_TYPE_PRIMARY) {
  319. cfg |= ATMEL_HLCDC_LAYER_OVR | ATMEL_HLCDC_LAYER_ITER2BL |
  320. ATMEL_HLCDC_LAYER_ITER;
  321. if (atmel_hlcdc_format_embeds_alpha(state->base.fb->pixel_format))
  322. cfg |= ATMEL_HLCDC_LAYER_LAEN;
  323. else
  324. cfg |= ATMEL_HLCDC_LAYER_GAEN |
  325. ATMEL_HLCDC_LAYER_GA(state->alpha);
  326. }
  327. atmel_hlcdc_layer_update_cfg(&plane->layer,
  328. ATMEL_HLCDC_LAYER_DMA_CFG_ID,
  329. ATMEL_HLCDC_LAYER_DMA_BLEN_MASK |
  330. ATMEL_HLCDC_LAYER_DMA_SIF,
  331. ATMEL_HLCDC_LAYER_DMA_BLEN_INCR16 |
  332. state->ahb_id);
  333. atmel_hlcdc_layer_update_cfg(&plane->layer, layout->general_config,
  334. ATMEL_HLCDC_LAYER_ITER2BL |
  335. ATMEL_HLCDC_LAYER_ITER |
  336. ATMEL_HLCDC_LAYER_GAEN |
  337. ATMEL_HLCDC_LAYER_GA_MASK |
  338. ATMEL_HLCDC_LAYER_LAEN |
  339. ATMEL_HLCDC_LAYER_OVR |
  340. ATMEL_HLCDC_LAYER_DMA, cfg);
  341. }
  342. static void atmel_hlcdc_plane_update_format(struct atmel_hlcdc_plane *plane,
  343. struct atmel_hlcdc_plane_state *state)
  344. {
  345. u32 cfg;
  346. int ret;
  347. ret = atmel_hlcdc_format_to_plane_mode(state->base.fb->pixel_format,
  348. &cfg);
  349. if (ret)
  350. return;
  351. if ((state->base.fb->pixel_format == DRM_FORMAT_YUV422 ||
  352. state->base.fb->pixel_format == DRM_FORMAT_NV61) &&
  353. (state->base.rotation & (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270))))
  354. cfg |= ATMEL_HLCDC_YUV422ROT;
  355. atmel_hlcdc_layer_update_cfg(&plane->layer,
  356. ATMEL_HLCDC_LAYER_FORMAT_CFG_ID,
  357. 0xffffffff,
  358. cfg);
  359. /*
  360. * Rotation optimization is not working on RGB888 (rotation is still
  361. * working but without any optimization).
  362. */
  363. if (state->base.fb->pixel_format == DRM_FORMAT_RGB888)
  364. cfg = ATMEL_HLCDC_LAYER_DMA_ROTDIS;
  365. else
  366. cfg = 0;
  367. atmel_hlcdc_layer_update_cfg(&plane->layer,
  368. ATMEL_HLCDC_LAYER_DMA_CFG_ID,
  369. ATMEL_HLCDC_LAYER_DMA_ROTDIS, cfg);
  370. }
  371. static void atmel_hlcdc_plane_update_buffers(struct atmel_hlcdc_plane *plane,
  372. struct atmel_hlcdc_plane_state *state)
  373. {
  374. struct atmel_hlcdc_layer *layer = &plane->layer;
  375. const struct atmel_hlcdc_layer_cfg_layout *layout =
  376. &layer->desc->layout;
  377. int i;
  378. atmel_hlcdc_layer_update_set_fb(&plane->layer, state->base.fb,
  379. state->offsets);
  380. for (i = 0; i < state->nplanes; i++) {
  381. if (layout->xstride[i]) {
  382. atmel_hlcdc_layer_update_cfg(&plane->layer,
  383. layout->xstride[i],
  384. 0xffffffff,
  385. state->xstride[i]);
  386. }
  387. if (layout->pstride[i]) {
  388. atmel_hlcdc_layer_update_cfg(&plane->layer,
  389. layout->pstride[i],
  390. 0xffffffff,
  391. state->pstride[i]);
  392. }
  393. }
  394. }
  395. int atmel_hlcdc_plane_prepare_ahb_routing(struct drm_crtc_state *c_state)
  396. {
  397. unsigned int ahb_load[2] = { };
  398. struct drm_plane *plane;
  399. drm_atomic_crtc_state_for_each_plane(plane, c_state) {
  400. struct atmel_hlcdc_plane_state *plane_state;
  401. struct drm_plane_state *plane_s;
  402. unsigned int pixels, load = 0;
  403. int i;
  404. plane_s = drm_atomic_get_plane_state(c_state->state, plane);
  405. if (IS_ERR(plane_s))
  406. return PTR_ERR(plane_s);
  407. plane_state =
  408. drm_plane_state_to_atmel_hlcdc_plane_state(plane_s);
  409. pixels = (plane_state->src_w * plane_state->src_h) -
  410. (plane_state->disc_w * plane_state->disc_h);
  411. for (i = 0; i < plane_state->nplanes; i++)
  412. load += pixels * plane_state->bpp[i];
  413. if (ahb_load[0] <= ahb_load[1])
  414. plane_state->ahb_id = 0;
  415. else
  416. plane_state->ahb_id = 1;
  417. ahb_load[plane_state->ahb_id] += load;
  418. }
  419. return 0;
  420. }
  421. int
  422. atmel_hlcdc_plane_prepare_disc_area(struct drm_crtc_state *c_state)
  423. {
  424. int disc_x = 0, disc_y = 0, disc_w = 0, disc_h = 0;
  425. const struct atmel_hlcdc_layer_cfg_layout *layout;
  426. struct atmel_hlcdc_plane_state *primary_state;
  427. struct drm_plane_state *primary_s;
  428. struct atmel_hlcdc_plane *primary;
  429. struct drm_plane *ovl;
  430. primary = drm_plane_to_atmel_hlcdc_plane(c_state->crtc->primary);
  431. layout = &primary->layer.desc->layout;
  432. if (!layout->disc_pos || !layout->disc_size)
  433. return 0;
  434. primary_s = drm_atomic_get_plane_state(c_state->state,
  435. &primary->base);
  436. if (IS_ERR(primary_s))
  437. return PTR_ERR(primary_s);
  438. primary_state = drm_plane_state_to_atmel_hlcdc_plane_state(primary_s);
  439. drm_atomic_crtc_state_for_each_plane(ovl, c_state) {
  440. struct atmel_hlcdc_plane_state *ovl_state;
  441. struct drm_plane_state *ovl_s;
  442. if (ovl == c_state->crtc->primary)
  443. continue;
  444. ovl_s = drm_atomic_get_plane_state(c_state->state, ovl);
  445. if (IS_ERR(ovl_s))
  446. return PTR_ERR(ovl_s);
  447. ovl_state = drm_plane_state_to_atmel_hlcdc_plane_state(ovl_s);
  448. if (!ovl_s->fb ||
  449. atmel_hlcdc_format_embeds_alpha(ovl_s->fb->pixel_format) ||
  450. ovl_state->alpha != 255)
  451. continue;
  452. /* TODO: implement a smarter hidden area detection */
  453. if (ovl_state->crtc_h * ovl_state->crtc_w < disc_h * disc_w)
  454. continue;
  455. disc_x = ovl_state->crtc_x;
  456. disc_y = ovl_state->crtc_y;
  457. disc_h = ovl_state->crtc_h;
  458. disc_w = ovl_state->crtc_w;
  459. }
  460. if (disc_x == primary_state->disc_x &&
  461. disc_y == primary_state->disc_y &&
  462. disc_w == primary_state->disc_w &&
  463. disc_h == primary_state->disc_h)
  464. return 0;
  465. primary_state->disc_x = disc_x;
  466. primary_state->disc_y = disc_y;
  467. primary_state->disc_w = disc_w;
  468. primary_state->disc_h = disc_h;
  469. primary_state->disc_updated = true;
  470. return 0;
  471. }
  472. static void
  473. atmel_hlcdc_plane_update_disc_area(struct atmel_hlcdc_plane *plane,
  474. struct atmel_hlcdc_plane_state *state)
  475. {
  476. const struct atmel_hlcdc_layer_cfg_layout *layout =
  477. &plane->layer.desc->layout;
  478. int disc_surface = 0;
  479. if (!state->disc_updated)
  480. return;
  481. disc_surface = state->disc_h * state->disc_w;
  482. atmel_hlcdc_layer_update_cfg(&plane->layer, layout->general_config,
  483. ATMEL_HLCDC_LAYER_DISCEN,
  484. disc_surface ? ATMEL_HLCDC_LAYER_DISCEN : 0);
  485. if (!disc_surface)
  486. return;
  487. atmel_hlcdc_layer_update_cfg(&plane->layer,
  488. layout->disc_pos,
  489. 0xffffffff,
  490. state->disc_x | (state->disc_y << 16));
  491. atmel_hlcdc_layer_update_cfg(&plane->layer,
  492. layout->disc_size,
  493. 0xffffffff,
  494. (state->disc_w - 1) |
  495. ((state->disc_h - 1) << 16));
  496. }
  497. static int atmel_hlcdc_plane_atomic_check(struct drm_plane *p,
  498. struct drm_plane_state *s)
  499. {
  500. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  501. struct atmel_hlcdc_plane_state *state =
  502. drm_plane_state_to_atmel_hlcdc_plane_state(s);
  503. const struct atmel_hlcdc_layer_cfg_layout *layout =
  504. &plane->layer.desc->layout;
  505. struct drm_framebuffer *fb = state->base.fb;
  506. const struct drm_display_mode *mode;
  507. struct drm_crtc_state *crtc_state;
  508. unsigned int patched_crtc_w;
  509. unsigned int patched_crtc_h;
  510. unsigned int patched_src_w;
  511. unsigned int patched_src_h;
  512. unsigned int tmp;
  513. int x_offset = 0;
  514. int y_offset = 0;
  515. int hsub = 1;
  516. int vsub = 1;
  517. int i;
  518. if (!state->base.crtc || !fb)
  519. return 0;
  520. crtc_state = drm_atomic_get_existing_crtc_state(s->state, s->crtc);
  521. mode = &crtc_state->adjusted_mode;
  522. state->src_x = s->src_x;
  523. state->src_y = s->src_y;
  524. state->src_h = s->src_h;
  525. state->src_w = s->src_w;
  526. state->crtc_x = s->crtc_x;
  527. state->crtc_y = s->crtc_y;
  528. state->crtc_h = s->crtc_h;
  529. state->crtc_w = s->crtc_w;
  530. if ((state->src_x | state->src_y | state->src_w | state->src_h) &
  531. SUBPIXEL_MASK)
  532. return -EINVAL;
  533. state->src_x >>= 16;
  534. state->src_y >>= 16;
  535. state->src_w >>= 16;
  536. state->src_h >>= 16;
  537. state->nplanes = drm_format_num_planes(fb->pixel_format);
  538. if (state->nplanes > ATMEL_HLCDC_MAX_PLANES)
  539. return -EINVAL;
  540. /*
  541. * Swap width and size in case of 90 or 270 degrees rotation
  542. */
  543. if (state->base.rotation & (BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270))) {
  544. tmp = state->crtc_w;
  545. state->crtc_w = state->crtc_h;
  546. state->crtc_h = tmp;
  547. tmp = state->src_w;
  548. state->src_w = state->src_h;
  549. state->src_h = tmp;
  550. }
  551. if (state->crtc_x + state->crtc_w > mode->hdisplay)
  552. patched_crtc_w = mode->hdisplay - state->crtc_x;
  553. else
  554. patched_crtc_w = state->crtc_w;
  555. if (state->crtc_x < 0) {
  556. patched_crtc_w += state->crtc_x;
  557. x_offset = -state->crtc_x;
  558. state->crtc_x = 0;
  559. }
  560. if (state->crtc_y + state->crtc_h > mode->vdisplay)
  561. patched_crtc_h = mode->vdisplay - state->crtc_y;
  562. else
  563. patched_crtc_h = state->crtc_h;
  564. if (state->crtc_y < 0) {
  565. patched_crtc_h += state->crtc_y;
  566. y_offset = -state->crtc_y;
  567. state->crtc_y = 0;
  568. }
  569. patched_src_w = DIV_ROUND_CLOSEST(patched_crtc_w * state->src_w,
  570. state->crtc_w);
  571. patched_src_h = DIV_ROUND_CLOSEST(patched_crtc_h * state->src_h,
  572. state->crtc_h);
  573. hsub = drm_format_horz_chroma_subsampling(fb->pixel_format);
  574. vsub = drm_format_vert_chroma_subsampling(fb->pixel_format);
  575. for (i = 0; i < state->nplanes; i++) {
  576. unsigned int offset = 0;
  577. int xdiv = i ? hsub : 1;
  578. int ydiv = i ? vsub : 1;
  579. state->bpp[i] = drm_format_plane_cpp(fb->pixel_format, i);
  580. if (!state->bpp[i])
  581. return -EINVAL;
  582. switch (state->base.rotation & DRM_ROTATE_MASK) {
  583. case BIT(DRM_ROTATE_90):
  584. offset = ((y_offset + state->src_y + patched_src_w - 1) /
  585. ydiv) * fb->pitches[i];
  586. offset += ((x_offset + state->src_x) / xdiv) *
  587. state->bpp[i];
  588. state->xstride[i] = ((patched_src_w - 1) / ydiv) *
  589. fb->pitches[i];
  590. state->pstride[i] = -fb->pitches[i] - state->bpp[i];
  591. break;
  592. case BIT(DRM_ROTATE_180):
  593. offset = ((y_offset + state->src_y + patched_src_h - 1) /
  594. ydiv) * fb->pitches[i];
  595. offset += ((x_offset + state->src_x + patched_src_w - 1) /
  596. xdiv) * state->bpp[i];
  597. state->xstride[i] = ((((patched_src_w - 1) / xdiv) - 1) *
  598. state->bpp[i]) - fb->pitches[i];
  599. state->pstride[i] = -2 * state->bpp[i];
  600. break;
  601. case BIT(DRM_ROTATE_270):
  602. offset = ((y_offset + state->src_y) / ydiv) *
  603. fb->pitches[i];
  604. offset += ((x_offset + state->src_x + patched_src_h - 1) /
  605. xdiv) * state->bpp[i];
  606. state->xstride[i] = -(((patched_src_w - 1) / ydiv) *
  607. fb->pitches[i]) -
  608. (2 * state->bpp[i]);
  609. state->pstride[i] = fb->pitches[i] - state->bpp[i];
  610. break;
  611. case BIT(DRM_ROTATE_0):
  612. default:
  613. offset = ((y_offset + state->src_y) / ydiv) *
  614. fb->pitches[i];
  615. offset += ((x_offset + state->src_x) / xdiv) *
  616. state->bpp[i];
  617. state->xstride[i] = fb->pitches[i] -
  618. ((patched_src_w / xdiv) *
  619. state->bpp[i]);
  620. state->pstride[i] = 0;
  621. break;
  622. }
  623. state->offsets[i] = offset + fb->offsets[i];
  624. }
  625. state->src_w = patched_src_w;
  626. state->src_h = patched_src_h;
  627. state->crtc_w = patched_crtc_w;
  628. state->crtc_h = patched_crtc_h;
  629. if (!layout->size &&
  630. (mode->hdisplay != state->crtc_w ||
  631. mode->vdisplay != state->crtc_h))
  632. return -EINVAL;
  633. if (plane->layer.desc->max_height &&
  634. state->crtc_h > plane->layer.desc->max_height)
  635. return -EINVAL;
  636. if (plane->layer.desc->max_width &&
  637. state->crtc_w > plane->layer.desc->max_width)
  638. return -EINVAL;
  639. if ((state->crtc_h != state->src_h || state->crtc_w != state->src_w) &&
  640. (!layout->memsize ||
  641. atmel_hlcdc_format_embeds_alpha(state->base.fb->pixel_format)))
  642. return -EINVAL;
  643. if (state->crtc_x < 0 || state->crtc_y < 0)
  644. return -EINVAL;
  645. if (state->crtc_w + state->crtc_x > mode->hdisplay ||
  646. state->crtc_h + state->crtc_y > mode->vdisplay)
  647. return -EINVAL;
  648. return 0;
  649. }
  650. static int atmel_hlcdc_plane_prepare_fb(struct drm_plane *p,
  651. const struct drm_plane_state *new_state)
  652. {
  653. /*
  654. * FIXME: we should avoid this const -> non-const cast but it's
  655. * currently the only solution we have to modify the ->prepared
  656. * state and rollback the update request.
  657. * Ideally, we should rework the code to attach all the resources
  658. * to atmel_hlcdc_plane_state (including the DMA desc allocation),
  659. * but this require a complete rework of the atmel_hlcdc_layer
  660. * code.
  661. */
  662. struct drm_plane_state *s = (struct drm_plane_state *)new_state;
  663. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  664. struct atmel_hlcdc_plane_state *state =
  665. drm_plane_state_to_atmel_hlcdc_plane_state(s);
  666. int ret;
  667. ret = atmel_hlcdc_layer_update_start(&plane->layer);
  668. if (!ret)
  669. state->prepared = true;
  670. return ret;
  671. }
  672. static void atmel_hlcdc_plane_cleanup_fb(struct drm_plane *p,
  673. const struct drm_plane_state *old_state)
  674. {
  675. /*
  676. * FIXME: we should avoid this const -> non-const cast but it's
  677. * currently the only solution we have to modify the ->prepared
  678. * state and rollback the update request.
  679. * Ideally, we should rework the code to attach all the resources
  680. * to atmel_hlcdc_plane_state (including the DMA desc allocation),
  681. * but this require a complete rework of the atmel_hlcdc_layer
  682. * code.
  683. */
  684. struct drm_plane_state *s = (struct drm_plane_state *)old_state;
  685. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  686. struct atmel_hlcdc_plane_state *state =
  687. drm_plane_state_to_atmel_hlcdc_plane_state(s);
  688. /*
  689. * The Request has already been applied or cancelled, nothing to do
  690. * here.
  691. */
  692. if (!state->prepared)
  693. return;
  694. atmel_hlcdc_layer_update_rollback(&plane->layer);
  695. state->prepared = false;
  696. }
  697. static void atmel_hlcdc_plane_atomic_update(struct drm_plane *p,
  698. struct drm_plane_state *old_s)
  699. {
  700. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  701. struct atmel_hlcdc_plane_state *state =
  702. drm_plane_state_to_atmel_hlcdc_plane_state(p->state);
  703. if (!p->state->crtc || !p->state->fb)
  704. return;
  705. atmel_hlcdc_plane_update_pos_and_size(plane, state);
  706. atmel_hlcdc_plane_update_general_settings(plane, state);
  707. atmel_hlcdc_plane_update_format(plane, state);
  708. atmel_hlcdc_plane_update_buffers(plane, state);
  709. atmel_hlcdc_plane_update_disc_area(plane, state);
  710. atmel_hlcdc_layer_update_commit(&plane->layer);
  711. }
  712. static void atmel_hlcdc_plane_atomic_disable(struct drm_plane *p,
  713. struct drm_plane_state *old_state)
  714. {
  715. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  716. atmel_hlcdc_layer_disable(&plane->layer);
  717. }
  718. static void atmel_hlcdc_plane_destroy(struct drm_plane *p)
  719. {
  720. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  721. if (plane->base.fb)
  722. drm_framebuffer_unreference(plane->base.fb);
  723. atmel_hlcdc_layer_cleanup(p->dev, &plane->layer);
  724. drm_plane_cleanup(p);
  725. devm_kfree(p->dev->dev, plane);
  726. }
  727. static int atmel_hlcdc_plane_atomic_set_property(struct drm_plane *p,
  728. struct drm_plane_state *s,
  729. struct drm_property *property,
  730. uint64_t val)
  731. {
  732. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  733. struct atmel_hlcdc_plane_properties *props = plane->properties;
  734. struct atmel_hlcdc_plane_state *state =
  735. drm_plane_state_to_atmel_hlcdc_plane_state(s);
  736. if (property == props->alpha)
  737. state->alpha = val;
  738. else
  739. return -EINVAL;
  740. return 0;
  741. }
  742. static int atmel_hlcdc_plane_atomic_get_property(struct drm_plane *p,
  743. const struct drm_plane_state *s,
  744. struct drm_property *property,
  745. uint64_t *val)
  746. {
  747. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  748. struct atmel_hlcdc_plane_properties *props = plane->properties;
  749. const struct atmel_hlcdc_plane_state *state =
  750. container_of(s, const struct atmel_hlcdc_plane_state, base);
  751. if (property == props->alpha)
  752. *val = state->alpha;
  753. else
  754. return -EINVAL;
  755. return 0;
  756. }
  757. static void atmel_hlcdc_plane_init_properties(struct atmel_hlcdc_plane *plane,
  758. const struct atmel_hlcdc_layer_desc *desc,
  759. struct atmel_hlcdc_plane_properties *props)
  760. {
  761. struct regmap *regmap = plane->layer.hlcdc->regmap;
  762. if (desc->type == ATMEL_HLCDC_OVERLAY_LAYER ||
  763. desc->type == ATMEL_HLCDC_CURSOR_LAYER) {
  764. drm_object_attach_property(&plane->base.base,
  765. props->alpha, 255);
  766. /* Set default alpha value */
  767. regmap_update_bits(regmap,
  768. desc->regs_offset +
  769. ATMEL_HLCDC_LAYER_GENERAL_CFG(&plane->layer),
  770. ATMEL_HLCDC_LAYER_GA_MASK,
  771. ATMEL_HLCDC_LAYER_GA_MASK);
  772. }
  773. if (desc->layout.xstride && desc->layout.pstride)
  774. drm_object_attach_property(&plane->base.base,
  775. plane->base.dev->mode_config.rotation_property,
  776. BIT(DRM_ROTATE_0));
  777. if (desc->layout.csc) {
  778. /*
  779. * TODO: decare a "yuv-to-rgb-conv-factors" property to let
  780. * userspace modify these factors (using a BLOB property ?).
  781. */
  782. regmap_write(regmap,
  783. desc->regs_offset +
  784. ATMEL_HLCDC_LAYER_CSC_CFG(&plane->layer, 0),
  785. 0x4c900091);
  786. regmap_write(regmap,
  787. desc->regs_offset +
  788. ATMEL_HLCDC_LAYER_CSC_CFG(&plane->layer, 1),
  789. 0x7a5f5090);
  790. regmap_write(regmap,
  791. desc->regs_offset +
  792. ATMEL_HLCDC_LAYER_CSC_CFG(&plane->layer, 2),
  793. 0x40040890);
  794. }
  795. }
  796. static struct drm_plane_helper_funcs atmel_hlcdc_layer_plane_helper_funcs = {
  797. .prepare_fb = atmel_hlcdc_plane_prepare_fb,
  798. .cleanup_fb = atmel_hlcdc_plane_cleanup_fb,
  799. .atomic_check = atmel_hlcdc_plane_atomic_check,
  800. .atomic_update = atmel_hlcdc_plane_atomic_update,
  801. .atomic_disable = atmel_hlcdc_plane_atomic_disable,
  802. };
  803. static void atmel_hlcdc_plane_reset(struct drm_plane *p)
  804. {
  805. struct atmel_hlcdc_plane_state *state;
  806. if (p->state) {
  807. state = drm_plane_state_to_atmel_hlcdc_plane_state(p->state);
  808. if (state->base.fb)
  809. drm_framebuffer_unreference(state->base.fb);
  810. kfree(state);
  811. p->state = NULL;
  812. }
  813. state = kzalloc(sizeof(*state), GFP_KERNEL);
  814. if (state) {
  815. state->alpha = 255;
  816. p->state = &state->base;
  817. p->state->plane = p;
  818. }
  819. }
  820. static struct drm_plane_state *
  821. atmel_hlcdc_plane_atomic_duplicate_state(struct drm_plane *p)
  822. {
  823. struct atmel_hlcdc_plane_state *state =
  824. drm_plane_state_to_atmel_hlcdc_plane_state(p->state);
  825. struct atmel_hlcdc_plane_state *copy;
  826. copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
  827. if (!copy)
  828. return NULL;
  829. copy->disc_updated = false;
  830. copy->prepared = false;
  831. if (copy->base.fb)
  832. drm_framebuffer_reference(copy->base.fb);
  833. return &copy->base;
  834. }
  835. static void atmel_hlcdc_plane_atomic_destroy_state(struct drm_plane *plane,
  836. struct drm_plane_state *s)
  837. {
  838. struct atmel_hlcdc_plane_state *state =
  839. drm_plane_state_to_atmel_hlcdc_plane_state(s);
  840. if (s->fb)
  841. drm_framebuffer_unreference(s->fb);
  842. kfree(state);
  843. }
  844. static struct drm_plane_funcs layer_plane_funcs = {
  845. .update_plane = drm_atomic_helper_update_plane,
  846. .disable_plane = drm_atomic_helper_disable_plane,
  847. .set_property = drm_atomic_helper_plane_set_property,
  848. .destroy = atmel_hlcdc_plane_destroy,
  849. .reset = atmel_hlcdc_plane_reset,
  850. .atomic_duplicate_state = atmel_hlcdc_plane_atomic_duplicate_state,
  851. .atomic_destroy_state = atmel_hlcdc_plane_atomic_destroy_state,
  852. .atomic_set_property = atmel_hlcdc_plane_atomic_set_property,
  853. .atomic_get_property = atmel_hlcdc_plane_atomic_get_property,
  854. };
  855. static struct atmel_hlcdc_plane *
  856. atmel_hlcdc_plane_create(struct drm_device *dev,
  857. const struct atmel_hlcdc_layer_desc *desc,
  858. struct atmel_hlcdc_plane_properties *props)
  859. {
  860. struct atmel_hlcdc_plane *plane;
  861. enum drm_plane_type type;
  862. int ret;
  863. plane = devm_kzalloc(dev->dev, sizeof(*plane), GFP_KERNEL);
  864. if (!plane)
  865. return ERR_PTR(-ENOMEM);
  866. ret = atmel_hlcdc_layer_init(dev, &plane->layer, desc);
  867. if (ret)
  868. return ERR_PTR(ret);
  869. if (desc->type == ATMEL_HLCDC_BASE_LAYER)
  870. type = DRM_PLANE_TYPE_PRIMARY;
  871. else if (desc->type == ATMEL_HLCDC_CURSOR_LAYER)
  872. type = DRM_PLANE_TYPE_CURSOR;
  873. else
  874. type = DRM_PLANE_TYPE_OVERLAY;
  875. ret = drm_universal_plane_init(dev, &plane->base, 0,
  876. &layer_plane_funcs,
  877. desc->formats->formats,
  878. desc->formats->nformats, type, NULL);
  879. if (ret)
  880. return ERR_PTR(ret);
  881. drm_plane_helper_add(&plane->base,
  882. &atmel_hlcdc_layer_plane_helper_funcs);
  883. /* Set default property values*/
  884. atmel_hlcdc_plane_init_properties(plane, desc, props);
  885. return plane;
  886. }
  887. static struct atmel_hlcdc_plane_properties *
  888. atmel_hlcdc_plane_create_properties(struct drm_device *dev)
  889. {
  890. struct atmel_hlcdc_plane_properties *props;
  891. props = devm_kzalloc(dev->dev, sizeof(*props), GFP_KERNEL);
  892. if (!props)
  893. return ERR_PTR(-ENOMEM);
  894. props->alpha = drm_property_create_range(dev, 0, "alpha", 0, 255);
  895. if (!props->alpha)
  896. return ERR_PTR(-ENOMEM);
  897. dev->mode_config.rotation_property =
  898. drm_mode_create_rotation_property(dev,
  899. BIT(DRM_ROTATE_0) |
  900. BIT(DRM_ROTATE_90) |
  901. BIT(DRM_ROTATE_180) |
  902. BIT(DRM_ROTATE_270));
  903. if (!dev->mode_config.rotation_property)
  904. return ERR_PTR(-ENOMEM);
  905. return props;
  906. }
  907. struct atmel_hlcdc_planes *
  908. atmel_hlcdc_create_planes(struct drm_device *dev)
  909. {
  910. struct atmel_hlcdc_dc *dc = dev->dev_private;
  911. struct atmel_hlcdc_plane_properties *props;
  912. struct atmel_hlcdc_planes *planes;
  913. const struct atmel_hlcdc_layer_desc *descs = dc->desc->layers;
  914. int nlayers = dc->desc->nlayers;
  915. int i;
  916. planes = devm_kzalloc(dev->dev, sizeof(*planes), GFP_KERNEL);
  917. if (!planes)
  918. return ERR_PTR(-ENOMEM);
  919. for (i = 0; i < nlayers; i++) {
  920. if (descs[i].type == ATMEL_HLCDC_OVERLAY_LAYER)
  921. planes->noverlays++;
  922. }
  923. if (planes->noverlays) {
  924. planes->overlays = devm_kzalloc(dev->dev,
  925. planes->noverlays *
  926. sizeof(*planes->overlays),
  927. GFP_KERNEL);
  928. if (!planes->overlays)
  929. return ERR_PTR(-ENOMEM);
  930. }
  931. props = atmel_hlcdc_plane_create_properties(dev);
  932. if (IS_ERR(props))
  933. return ERR_CAST(props);
  934. planes->noverlays = 0;
  935. for (i = 0; i < nlayers; i++) {
  936. struct atmel_hlcdc_plane *plane;
  937. if (descs[i].type == ATMEL_HLCDC_PP_LAYER)
  938. continue;
  939. plane = atmel_hlcdc_plane_create(dev, &descs[i], props);
  940. if (IS_ERR(plane))
  941. return ERR_CAST(plane);
  942. plane->properties = props;
  943. switch (descs[i].type) {
  944. case ATMEL_HLCDC_BASE_LAYER:
  945. if (planes->primary)
  946. return ERR_PTR(-EINVAL);
  947. planes->primary = plane;
  948. break;
  949. case ATMEL_HLCDC_OVERLAY_LAYER:
  950. planes->overlays[planes->noverlays++] = plane;
  951. break;
  952. case ATMEL_HLCDC_CURSOR_LAYER:
  953. if (planes->cursor)
  954. return ERR_PTR(-EINVAL);
  955. planes->cursor = plane;
  956. break;
  957. default:
  958. break;
  959. }
  960. }
  961. return planes;
  962. }