atmel_hlcdc_plane.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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. * @disc_x: x discard position
  33. * @disc_y: y discard position
  34. * @disc_w: discard width
  35. * @disc_h: discard height
  36. * @bpp: bytes per pixel deduced from pixel_format
  37. * @offsets: offsets to apply to the GEM buffers
  38. * @xstride: value to add to the pixel pointer between each line
  39. * @pstride: value to add to the pixel pointer between each pixel
  40. * @nplanes: number of planes (deduced from pixel_format)
  41. * @dscrs: DMA descriptors
  42. */
  43. struct atmel_hlcdc_plane_state {
  44. struct drm_plane_state base;
  45. int crtc_x;
  46. int crtc_y;
  47. unsigned int crtc_w;
  48. unsigned int crtc_h;
  49. uint32_t src_x;
  50. uint32_t src_y;
  51. uint32_t src_w;
  52. uint32_t src_h;
  53. int disc_x;
  54. int disc_y;
  55. int disc_w;
  56. int disc_h;
  57. int ahb_id;
  58. /* These fields are private and should not be touched */
  59. int bpp[ATMEL_HLCDC_LAYER_MAX_PLANES];
  60. unsigned int offsets[ATMEL_HLCDC_LAYER_MAX_PLANES];
  61. int xstride[ATMEL_HLCDC_LAYER_MAX_PLANES];
  62. int pstride[ATMEL_HLCDC_LAYER_MAX_PLANES];
  63. int nplanes;
  64. /* DMA descriptors. */
  65. struct atmel_hlcdc_dma_channel_dscr *dscrs[ATMEL_HLCDC_LAYER_MAX_PLANES];
  66. };
  67. static inline struct atmel_hlcdc_plane_state *
  68. drm_plane_state_to_atmel_hlcdc_plane_state(struct drm_plane_state *s)
  69. {
  70. return container_of(s, struct atmel_hlcdc_plane_state, base);
  71. }
  72. #define SUBPIXEL_MASK 0xffff
  73. static uint32_t rgb_formats[] = {
  74. DRM_FORMAT_C8,
  75. DRM_FORMAT_XRGB4444,
  76. DRM_FORMAT_ARGB4444,
  77. DRM_FORMAT_RGBA4444,
  78. DRM_FORMAT_ARGB1555,
  79. DRM_FORMAT_RGB565,
  80. DRM_FORMAT_RGB888,
  81. DRM_FORMAT_XRGB8888,
  82. DRM_FORMAT_ARGB8888,
  83. DRM_FORMAT_RGBA8888,
  84. };
  85. struct atmel_hlcdc_formats atmel_hlcdc_plane_rgb_formats = {
  86. .formats = rgb_formats,
  87. .nformats = ARRAY_SIZE(rgb_formats),
  88. };
  89. static uint32_t rgb_and_yuv_formats[] = {
  90. DRM_FORMAT_C8,
  91. DRM_FORMAT_XRGB4444,
  92. DRM_FORMAT_ARGB4444,
  93. DRM_FORMAT_RGBA4444,
  94. DRM_FORMAT_ARGB1555,
  95. DRM_FORMAT_RGB565,
  96. DRM_FORMAT_RGB888,
  97. DRM_FORMAT_XRGB8888,
  98. DRM_FORMAT_ARGB8888,
  99. DRM_FORMAT_RGBA8888,
  100. DRM_FORMAT_AYUV,
  101. DRM_FORMAT_YUYV,
  102. DRM_FORMAT_UYVY,
  103. DRM_FORMAT_YVYU,
  104. DRM_FORMAT_VYUY,
  105. DRM_FORMAT_NV21,
  106. DRM_FORMAT_NV61,
  107. DRM_FORMAT_YUV422,
  108. DRM_FORMAT_YUV420,
  109. };
  110. struct atmel_hlcdc_formats atmel_hlcdc_plane_rgb_and_yuv_formats = {
  111. .formats = rgb_and_yuv_formats,
  112. .nformats = ARRAY_SIZE(rgb_and_yuv_formats),
  113. };
  114. static int atmel_hlcdc_format_to_plane_mode(u32 format, u32 *mode)
  115. {
  116. switch (format) {
  117. case DRM_FORMAT_C8:
  118. *mode = ATMEL_HLCDC_C8_MODE;
  119. break;
  120. case DRM_FORMAT_XRGB4444:
  121. *mode = ATMEL_HLCDC_XRGB4444_MODE;
  122. break;
  123. case DRM_FORMAT_ARGB4444:
  124. *mode = ATMEL_HLCDC_ARGB4444_MODE;
  125. break;
  126. case DRM_FORMAT_RGBA4444:
  127. *mode = ATMEL_HLCDC_RGBA4444_MODE;
  128. break;
  129. case DRM_FORMAT_RGB565:
  130. *mode = ATMEL_HLCDC_RGB565_MODE;
  131. break;
  132. case DRM_FORMAT_RGB888:
  133. *mode = ATMEL_HLCDC_RGB888_MODE;
  134. break;
  135. case DRM_FORMAT_ARGB1555:
  136. *mode = ATMEL_HLCDC_ARGB1555_MODE;
  137. break;
  138. case DRM_FORMAT_XRGB8888:
  139. *mode = ATMEL_HLCDC_XRGB8888_MODE;
  140. break;
  141. case DRM_FORMAT_ARGB8888:
  142. *mode = ATMEL_HLCDC_ARGB8888_MODE;
  143. break;
  144. case DRM_FORMAT_RGBA8888:
  145. *mode = ATMEL_HLCDC_RGBA8888_MODE;
  146. break;
  147. case DRM_FORMAT_AYUV:
  148. *mode = ATMEL_HLCDC_AYUV_MODE;
  149. break;
  150. case DRM_FORMAT_YUYV:
  151. *mode = ATMEL_HLCDC_YUYV_MODE;
  152. break;
  153. case DRM_FORMAT_UYVY:
  154. *mode = ATMEL_HLCDC_UYVY_MODE;
  155. break;
  156. case DRM_FORMAT_YVYU:
  157. *mode = ATMEL_HLCDC_YVYU_MODE;
  158. break;
  159. case DRM_FORMAT_VYUY:
  160. *mode = ATMEL_HLCDC_VYUY_MODE;
  161. break;
  162. case DRM_FORMAT_NV21:
  163. *mode = ATMEL_HLCDC_NV21_MODE;
  164. break;
  165. case DRM_FORMAT_NV61:
  166. *mode = ATMEL_HLCDC_NV61_MODE;
  167. break;
  168. case DRM_FORMAT_YUV420:
  169. *mode = ATMEL_HLCDC_YUV420_MODE;
  170. break;
  171. case DRM_FORMAT_YUV422:
  172. *mode = ATMEL_HLCDC_YUV422_MODE;
  173. break;
  174. default:
  175. return -ENOTSUPP;
  176. }
  177. return 0;
  178. }
  179. static u32 heo_downscaling_xcoef[] = {
  180. 0x11343311,
  181. 0x000000f7,
  182. 0x1635300c,
  183. 0x000000f9,
  184. 0x1b362c08,
  185. 0x000000fb,
  186. 0x1f372804,
  187. 0x000000fe,
  188. 0x24382400,
  189. 0x00000000,
  190. 0x28371ffe,
  191. 0x00000004,
  192. 0x2c361bfb,
  193. 0x00000008,
  194. 0x303516f9,
  195. 0x0000000c,
  196. };
  197. static u32 heo_downscaling_ycoef[] = {
  198. 0x00123737,
  199. 0x00173732,
  200. 0x001b382d,
  201. 0x001f3928,
  202. 0x00243824,
  203. 0x0028391f,
  204. 0x002d381b,
  205. 0x00323717,
  206. };
  207. static u32 heo_upscaling_xcoef[] = {
  208. 0xf74949f7,
  209. 0x00000000,
  210. 0xf55f33fb,
  211. 0x000000fe,
  212. 0xf5701efe,
  213. 0x000000ff,
  214. 0xf87c0dff,
  215. 0x00000000,
  216. 0x00800000,
  217. 0x00000000,
  218. 0x0d7cf800,
  219. 0x000000ff,
  220. 0x1e70f5ff,
  221. 0x000000fe,
  222. 0x335ff5fe,
  223. 0x000000fb,
  224. };
  225. static u32 heo_upscaling_ycoef[] = {
  226. 0x00004040,
  227. 0x00075920,
  228. 0x00056f0c,
  229. 0x00027b03,
  230. 0x00008000,
  231. 0x00037b02,
  232. 0x000c6f05,
  233. 0x00205907,
  234. };
  235. #define ATMEL_HLCDC_XPHIDEF 4
  236. #define ATMEL_HLCDC_YPHIDEF 4
  237. static u32 atmel_hlcdc_plane_phiscaler_get_factor(u32 srcsize,
  238. u32 dstsize,
  239. u32 phidef)
  240. {
  241. u32 factor, max_memsize;
  242. factor = (256 * ((8 * (srcsize - 1)) - phidef)) / (dstsize - 1);
  243. max_memsize = ((factor * (dstsize - 1)) + (256 * phidef)) / 2048;
  244. if (max_memsize > srcsize - 1)
  245. factor--;
  246. return factor;
  247. }
  248. static void
  249. atmel_hlcdc_plane_scaler_set_phicoeff(struct atmel_hlcdc_plane *plane,
  250. const u32 *coeff_tab, int size,
  251. unsigned int cfg_offs)
  252. {
  253. int i;
  254. for (i = 0; i < size; i++)
  255. atmel_hlcdc_layer_write_cfg(&plane->layer, cfg_offs + i,
  256. coeff_tab[i]);
  257. }
  258. void atmel_hlcdc_plane_setup_scaler(struct atmel_hlcdc_plane *plane,
  259. struct atmel_hlcdc_plane_state *state)
  260. {
  261. const struct atmel_hlcdc_layer_desc *desc = plane->layer.desc;
  262. u32 xfactor, yfactor;
  263. if (!desc->layout.scaler_config)
  264. return;
  265. if (state->crtc_w == state->src_w && state->crtc_h == state->src_h) {
  266. atmel_hlcdc_layer_write_cfg(&plane->layer,
  267. desc->layout.scaler_config, 0);
  268. return;
  269. }
  270. if (desc->layout.phicoeffs.x) {
  271. xfactor = atmel_hlcdc_plane_phiscaler_get_factor(state->src_w,
  272. state->crtc_w,
  273. ATMEL_HLCDC_XPHIDEF);
  274. yfactor = atmel_hlcdc_plane_phiscaler_get_factor(state->src_h,
  275. state->crtc_h,
  276. ATMEL_HLCDC_YPHIDEF);
  277. atmel_hlcdc_plane_scaler_set_phicoeff(plane,
  278. state->crtc_w < state->src_w ?
  279. heo_downscaling_xcoef :
  280. heo_upscaling_xcoef,
  281. ARRAY_SIZE(heo_upscaling_xcoef),
  282. desc->layout.phicoeffs.x);
  283. atmel_hlcdc_plane_scaler_set_phicoeff(plane,
  284. state->crtc_h < state->src_h ?
  285. heo_downscaling_ycoef :
  286. heo_upscaling_ycoef,
  287. ARRAY_SIZE(heo_upscaling_ycoef),
  288. desc->layout.phicoeffs.y);
  289. } else {
  290. xfactor = (1024 * state->src_w) / state->crtc_w;
  291. yfactor = (1024 * state->src_h) / state->crtc_h;
  292. }
  293. atmel_hlcdc_layer_write_cfg(&plane->layer, desc->layout.scaler_config,
  294. ATMEL_HLCDC_LAYER_SCALER_ENABLE |
  295. ATMEL_HLCDC_LAYER_SCALER_FACTORS(xfactor,
  296. yfactor));
  297. }
  298. static void
  299. atmel_hlcdc_plane_update_pos_and_size(struct atmel_hlcdc_plane *plane,
  300. struct atmel_hlcdc_plane_state *state)
  301. {
  302. const struct atmel_hlcdc_layer_desc *desc = plane->layer.desc;
  303. if (desc->layout.size)
  304. atmel_hlcdc_layer_write_cfg(&plane->layer, desc->layout.size,
  305. ATMEL_HLCDC_LAYER_SIZE(state->crtc_w,
  306. state->crtc_h));
  307. if (desc->layout.memsize)
  308. atmel_hlcdc_layer_write_cfg(&plane->layer,
  309. desc->layout.memsize,
  310. ATMEL_HLCDC_LAYER_SIZE(state->src_w,
  311. state->src_h));
  312. if (desc->layout.pos)
  313. atmel_hlcdc_layer_write_cfg(&plane->layer, desc->layout.pos,
  314. ATMEL_HLCDC_LAYER_POS(state->crtc_x,
  315. state->crtc_y));
  316. atmel_hlcdc_plane_setup_scaler(plane, state);
  317. }
  318. static void
  319. atmel_hlcdc_plane_update_general_settings(struct atmel_hlcdc_plane *plane,
  320. struct atmel_hlcdc_plane_state *state)
  321. {
  322. unsigned int cfg = ATMEL_HLCDC_LAYER_DMA_BLEN_INCR16 | state->ahb_id;
  323. const struct atmel_hlcdc_layer_desc *desc = plane->layer.desc;
  324. const struct drm_format_info *format = state->base.fb->format;
  325. /*
  326. * Rotation optimization is not working on RGB888 (rotation is still
  327. * working but without any optimization).
  328. */
  329. if (format->format == DRM_FORMAT_RGB888)
  330. cfg |= ATMEL_HLCDC_LAYER_DMA_ROTDIS;
  331. atmel_hlcdc_layer_write_cfg(&plane->layer, ATMEL_HLCDC_LAYER_DMA_CFG,
  332. cfg);
  333. cfg = ATMEL_HLCDC_LAYER_DMA;
  334. if (plane->base.type != DRM_PLANE_TYPE_PRIMARY) {
  335. cfg |= ATMEL_HLCDC_LAYER_OVR | ATMEL_HLCDC_LAYER_ITER2BL |
  336. ATMEL_HLCDC_LAYER_ITER;
  337. if (format->has_alpha)
  338. cfg |= ATMEL_HLCDC_LAYER_LAEN;
  339. else
  340. cfg |= ATMEL_HLCDC_LAYER_GAEN |
  341. ATMEL_HLCDC_LAYER_GA(state->base.alpha >> 8);
  342. }
  343. if (state->disc_h && state->disc_w)
  344. cfg |= ATMEL_HLCDC_LAYER_DISCEN;
  345. atmel_hlcdc_layer_write_cfg(&plane->layer, desc->layout.general_config,
  346. cfg);
  347. }
  348. static void atmel_hlcdc_plane_update_format(struct atmel_hlcdc_plane *plane,
  349. struct atmel_hlcdc_plane_state *state)
  350. {
  351. u32 cfg;
  352. int ret;
  353. ret = atmel_hlcdc_format_to_plane_mode(state->base.fb->format->format,
  354. &cfg);
  355. if (ret)
  356. return;
  357. if ((state->base.fb->format->format == DRM_FORMAT_YUV422 ||
  358. state->base.fb->format->format == DRM_FORMAT_NV61) &&
  359. drm_rotation_90_or_270(state->base.rotation))
  360. cfg |= ATMEL_HLCDC_YUV422ROT;
  361. atmel_hlcdc_layer_write_cfg(&plane->layer,
  362. ATMEL_HLCDC_LAYER_FORMAT_CFG, cfg);
  363. }
  364. static void atmel_hlcdc_plane_update_clut(struct atmel_hlcdc_plane *plane,
  365. struct atmel_hlcdc_plane_state *state)
  366. {
  367. struct drm_crtc *crtc = state->base.crtc;
  368. struct drm_color_lut *lut;
  369. int idx;
  370. if (!crtc || !crtc->state)
  371. return;
  372. if (!crtc->state->color_mgmt_changed || !crtc->state->gamma_lut)
  373. return;
  374. lut = (struct drm_color_lut *)crtc->state->gamma_lut->data;
  375. for (idx = 0; idx < ATMEL_HLCDC_CLUT_SIZE; idx++, lut++) {
  376. u32 val = ((lut->red << 8) & 0xff0000) |
  377. (lut->green & 0xff00) |
  378. (lut->blue >> 8);
  379. atmel_hlcdc_layer_write_clut(&plane->layer, idx, val);
  380. }
  381. }
  382. static void atmel_hlcdc_plane_update_buffers(struct atmel_hlcdc_plane *plane,
  383. struct atmel_hlcdc_plane_state *state)
  384. {
  385. const struct atmel_hlcdc_layer_desc *desc = plane->layer.desc;
  386. struct drm_framebuffer *fb = state->base.fb;
  387. u32 sr;
  388. int i;
  389. sr = atmel_hlcdc_layer_read_reg(&plane->layer, ATMEL_HLCDC_LAYER_CHSR);
  390. for (i = 0; i < state->nplanes; i++) {
  391. struct drm_gem_cma_object *gem = drm_fb_cma_get_gem_obj(fb, i);
  392. state->dscrs[i]->addr = gem->paddr + state->offsets[i];
  393. atmel_hlcdc_layer_write_reg(&plane->layer,
  394. ATMEL_HLCDC_LAYER_PLANE_HEAD(i),
  395. state->dscrs[i]->self);
  396. if (!(sr & ATMEL_HLCDC_LAYER_EN)) {
  397. atmel_hlcdc_layer_write_reg(&plane->layer,
  398. ATMEL_HLCDC_LAYER_PLANE_ADDR(i),
  399. state->dscrs[i]->addr);
  400. atmel_hlcdc_layer_write_reg(&plane->layer,
  401. ATMEL_HLCDC_LAYER_PLANE_CTRL(i),
  402. state->dscrs[i]->ctrl);
  403. atmel_hlcdc_layer_write_reg(&plane->layer,
  404. ATMEL_HLCDC_LAYER_PLANE_NEXT(i),
  405. state->dscrs[i]->self);
  406. }
  407. if (desc->layout.xstride[i])
  408. atmel_hlcdc_layer_write_cfg(&plane->layer,
  409. desc->layout.xstride[i],
  410. state->xstride[i]);
  411. if (desc->layout.pstride[i])
  412. atmel_hlcdc_layer_write_cfg(&plane->layer,
  413. desc->layout.pstride[i],
  414. state->pstride[i]);
  415. }
  416. }
  417. int atmel_hlcdc_plane_prepare_ahb_routing(struct drm_crtc_state *c_state)
  418. {
  419. unsigned int ahb_load[2] = { };
  420. struct drm_plane *plane;
  421. drm_atomic_crtc_state_for_each_plane(plane, c_state) {
  422. struct atmel_hlcdc_plane_state *plane_state;
  423. struct drm_plane_state *plane_s;
  424. unsigned int pixels, load = 0;
  425. int i;
  426. plane_s = drm_atomic_get_plane_state(c_state->state, plane);
  427. if (IS_ERR(plane_s))
  428. return PTR_ERR(plane_s);
  429. plane_state =
  430. drm_plane_state_to_atmel_hlcdc_plane_state(plane_s);
  431. pixels = (plane_state->src_w * plane_state->src_h) -
  432. (plane_state->disc_w * plane_state->disc_h);
  433. for (i = 0; i < plane_state->nplanes; i++)
  434. load += pixels * plane_state->bpp[i];
  435. if (ahb_load[0] <= ahb_load[1])
  436. plane_state->ahb_id = 0;
  437. else
  438. plane_state->ahb_id = 1;
  439. ahb_load[plane_state->ahb_id] += load;
  440. }
  441. return 0;
  442. }
  443. int
  444. atmel_hlcdc_plane_prepare_disc_area(struct drm_crtc_state *c_state)
  445. {
  446. int disc_x = 0, disc_y = 0, disc_w = 0, disc_h = 0;
  447. const struct atmel_hlcdc_layer_cfg_layout *layout;
  448. struct atmel_hlcdc_plane_state *primary_state;
  449. struct drm_plane_state *primary_s;
  450. struct atmel_hlcdc_plane *primary;
  451. struct drm_plane *ovl;
  452. primary = drm_plane_to_atmel_hlcdc_plane(c_state->crtc->primary);
  453. layout = &primary->layer.desc->layout;
  454. if (!layout->disc_pos || !layout->disc_size)
  455. return 0;
  456. primary_s = drm_atomic_get_plane_state(c_state->state,
  457. &primary->base);
  458. if (IS_ERR(primary_s))
  459. return PTR_ERR(primary_s);
  460. primary_state = drm_plane_state_to_atmel_hlcdc_plane_state(primary_s);
  461. drm_atomic_crtc_state_for_each_plane(ovl, c_state) {
  462. struct atmel_hlcdc_plane_state *ovl_state;
  463. struct drm_plane_state *ovl_s;
  464. if (ovl == c_state->crtc->primary)
  465. continue;
  466. ovl_s = drm_atomic_get_plane_state(c_state->state, ovl);
  467. if (IS_ERR(ovl_s))
  468. return PTR_ERR(ovl_s);
  469. ovl_state = drm_plane_state_to_atmel_hlcdc_plane_state(ovl_s);
  470. if (!ovl_s->fb ||
  471. ovl_s->fb->format->has_alpha ||
  472. ovl_s->alpha != DRM_BLEND_ALPHA_OPAQUE)
  473. continue;
  474. /* TODO: implement a smarter hidden area detection */
  475. if (ovl_state->crtc_h * ovl_state->crtc_w < disc_h * disc_w)
  476. continue;
  477. disc_x = ovl_state->crtc_x;
  478. disc_y = ovl_state->crtc_y;
  479. disc_h = ovl_state->crtc_h;
  480. disc_w = ovl_state->crtc_w;
  481. }
  482. primary_state->disc_x = disc_x;
  483. primary_state->disc_y = disc_y;
  484. primary_state->disc_w = disc_w;
  485. primary_state->disc_h = disc_h;
  486. return 0;
  487. }
  488. static void
  489. atmel_hlcdc_plane_update_disc_area(struct atmel_hlcdc_plane *plane,
  490. struct atmel_hlcdc_plane_state *state)
  491. {
  492. const struct atmel_hlcdc_layer_cfg_layout *layout;
  493. layout = &plane->layer.desc->layout;
  494. if (!layout->disc_pos || !layout->disc_size)
  495. return;
  496. atmel_hlcdc_layer_write_cfg(&plane->layer, layout->disc_pos,
  497. ATMEL_HLCDC_LAYER_DISC_POS(state->disc_x,
  498. state->disc_y));
  499. atmel_hlcdc_layer_write_cfg(&plane->layer, layout->disc_size,
  500. ATMEL_HLCDC_LAYER_DISC_SIZE(state->disc_w,
  501. state->disc_h));
  502. }
  503. static int atmel_hlcdc_plane_atomic_check(struct drm_plane *p,
  504. struct drm_plane_state *s)
  505. {
  506. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  507. struct atmel_hlcdc_plane_state *state =
  508. drm_plane_state_to_atmel_hlcdc_plane_state(s);
  509. const struct atmel_hlcdc_layer_desc *desc = plane->layer.desc;
  510. struct drm_framebuffer *fb = state->base.fb;
  511. const struct drm_display_mode *mode;
  512. struct drm_crtc_state *crtc_state;
  513. unsigned int patched_crtc_w;
  514. unsigned int patched_crtc_h;
  515. unsigned int patched_src_w;
  516. unsigned int patched_src_h;
  517. unsigned int tmp;
  518. int x_offset = 0;
  519. int y_offset = 0;
  520. int hsub = 1;
  521. int vsub = 1;
  522. int i;
  523. if (!state->base.crtc || !fb)
  524. return 0;
  525. crtc_state = drm_atomic_get_existing_crtc_state(s->state, s->crtc);
  526. mode = &crtc_state->adjusted_mode;
  527. state->src_x = s->src_x;
  528. state->src_y = s->src_y;
  529. state->src_h = s->src_h;
  530. state->src_w = s->src_w;
  531. state->crtc_x = s->crtc_x;
  532. state->crtc_y = s->crtc_y;
  533. state->crtc_h = s->crtc_h;
  534. state->crtc_w = s->crtc_w;
  535. if ((state->src_x | state->src_y | state->src_w | state->src_h) &
  536. SUBPIXEL_MASK)
  537. return -EINVAL;
  538. state->src_x >>= 16;
  539. state->src_y >>= 16;
  540. state->src_w >>= 16;
  541. state->src_h >>= 16;
  542. state->nplanes = fb->format->num_planes;
  543. if (state->nplanes > ATMEL_HLCDC_LAYER_MAX_PLANES)
  544. return -EINVAL;
  545. /*
  546. * Swap width and size in case of 90 or 270 degrees rotation
  547. */
  548. if (drm_rotation_90_or_270(state->base.rotation)) {
  549. tmp = state->crtc_w;
  550. state->crtc_w = state->crtc_h;
  551. state->crtc_h = tmp;
  552. tmp = state->src_w;
  553. state->src_w = state->src_h;
  554. state->src_h = tmp;
  555. }
  556. if (state->crtc_x + state->crtc_w > mode->hdisplay)
  557. patched_crtc_w = mode->hdisplay - state->crtc_x;
  558. else
  559. patched_crtc_w = state->crtc_w;
  560. if (state->crtc_x < 0) {
  561. patched_crtc_w += state->crtc_x;
  562. x_offset = -state->crtc_x;
  563. state->crtc_x = 0;
  564. }
  565. if (state->crtc_y + state->crtc_h > mode->vdisplay)
  566. patched_crtc_h = mode->vdisplay - state->crtc_y;
  567. else
  568. patched_crtc_h = state->crtc_h;
  569. if (state->crtc_y < 0) {
  570. patched_crtc_h += state->crtc_y;
  571. y_offset = -state->crtc_y;
  572. state->crtc_y = 0;
  573. }
  574. patched_src_w = DIV_ROUND_CLOSEST(patched_crtc_w * state->src_w,
  575. state->crtc_w);
  576. patched_src_h = DIV_ROUND_CLOSEST(patched_crtc_h * state->src_h,
  577. state->crtc_h);
  578. hsub = drm_format_horz_chroma_subsampling(fb->format->format);
  579. vsub = drm_format_vert_chroma_subsampling(fb->format->format);
  580. for (i = 0; i < state->nplanes; i++) {
  581. unsigned int offset = 0;
  582. int xdiv = i ? hsub : 1;
  583. int ydiv = i ? vsub : 1;
  584. state->bpp[i] = fb->format->cpp[i];
  585. if (!state->bpp[i])
  586. return -EINVAL;
  587. switch (state->base.rotation & DRM_MODE_ROTATE_MASK) {
  588. case DRM_MODE_ROTATE_90:
  589. offset = ((y_offset + state->src_y + patched_src_w - 1) /
  590. ydiv) * fb->pitches[i];
  591. offset += ((x_offset + state->src_x) / xdiv) *
  592. state->bpp[i];
  593. state->xstride[i] = ((patched_src_w - 1) / ydiv) *
  594. fb->pitches[i];
  595. state->pstride[i] = -fb->pitches[i] - state->bpp[i];
  596. break;
  597. case DRM_MODE_ROTATE_180:
  598. offset = ((y_offset + state->src_y + patched_src_h - 1) /
  599. ydiv) * fb->pitches[i];
  600. offset += ((x_offset + state->src_x + patched_src_w - 1) /
  601. xdiv) * state->bpp[i];
  602. state->xstride[i] = ((((patched_src_w - 1) / xdiv) - 1) *
  603. state->bpp[i]) - fb->pitches[i];
  604. state->pstride[i] = -2 * state->bpp[i];
  605. break;
  606. case DRM_MODE_ROTATE_270:
  607. offset = ((y_offset + state->src_y) / ydiv) *
  608. fb->pitches[i];
  609. offset += ((x_offset + state->src_x + patched_src_h - 1) /
  610. xdiv) * state->bpp[i];
  611. state->xstride[i] = -(((patched_src_w - 1) / ydiv) *
  612. fb->pitches[i]) -
  613. (2 * state->bpp[i]);
  614. state->pstride[i] = fb->pitches[i] - state->bpp[i];
  615. break;
  616. case DRM_MODE_ROTATE_0:
  617. default:
  618. offset = ((y_offset + state->src_y) / ydiv) *
  619. fb->pitches[i];
  620. offset += ((x_offset + state->src_x) / xdiv) *
  621. state->bpp[i];
  622. state->xstride[i] = fb->pitches[i] -
  623. ((patched_src_w / xdiv) *
  624. state->bpp[i]);
  625. state->pstride[i] = 0;
  626. break;
  627. }
  628. state->offsets[i] = offset + fb->offsets[i];
  629. }
  630. state->src_w = patched_src_w;
  631. state->src_h = patched_src_h;
  632. state->crtc_w = patched_crtc_w;
  633. state->crtc_h = patched_crtc_h;
  634. if (!desc->layout.size &&
  635. (mode->hdisplay != state->crtc_w ||
  636. mode->vdisplay != state->crtc_h))
  637. return -EINVAL;
  638. if (desc->max_height && state->crtc_h > desc->max_height)
  639. return -EINVAL;
  640. if (desc->max_width && state->crtc_w > desc->max_width)
  641. return -EINVAL;
  642. if ((state->crtc_h != state->src_h || state->crtc_w != state->src_w) &&
  643. (!desc->layout.memsize ||
  644. state->base.fb->format->has_alpha))
  645. return -EINVAL;
  646. if (state->crtc_x < 0 || state->crtc_y < 0)
  647. return -EINVAL;
  648. if (state->crtc_w + state->crtc_x > mode->hdisplay ||
  649. state->crtc_h + state->crtc_y > mode->vdisplay)
  650. return -EINVAL;
  651. return 0;
  652. }
  653. static void atmel_hlcdc_plane_atomic_update(struct drm_plane *p,
  654. struct drm_plane_state *old_s)
  655. {
  656. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  657. struct atmel_hlcdc_plane_state *state =
  658. drm_plane_state_to_atmel_hlcdc_plane_state(p->state);
  659. u32 sr;
  660. if (!p->state->crtc || !p->state->fb)
  661. return;
  662. atmel_hlcdc_plane_update_pos_and_size(plane, state);
  663. atmel_hlcdc_plane_update_general_settings(plane, state);
  664. atmel_hlcdc_plane_update_format(plane, state);
  665. atmel_hlcdc_plane_update_clut(plane, state);
  666. atmel_hlcdc_plane_update_buffers(plane, state);
  667. atmel_hlcdc_plane_update_disc_area(plane, state);
  668. /* Enable the overrun interrupts. */
  669. atmel_hlcdc_layer_write_reg(&plane->layer, ATMEL_HLCDC_LAYER_IER,
  670. ATMEL_HLCDC_LAYER_OVR_IRQ(0) |
  671. ATMEL_HLCDC_LAYER_OVR_IRQ(1) |
  672. ATMEL_HLCDC_LAYER_OVR_IRQ(2));
  673. /* Apply the new config at the next SOF event. */
  674. sr = atmel_hlcdc_layer_read_reg(&plane->layer, ATMEL_HLCDC_LAYER_CHSR);
  675. atmel_hlcdc_layer_write_reg(&plane->layer, ATMEL_HLCDC_LAYER_CHER,
  676. ATMEL_HLCDC_LAYER_UPDATE |
  677. (sr & ATMEL_HLCDC_LAYER_EN ?
  678. ATMEL_HLCDC_LAYER_A2Q : ATMEL_HLCDC_LAYER_EN));
  679. }
  680. static void atmel_hlcdc_plane_atomic_disable(struct drm_plane *p,
  681. struct drm_plane_state *old_state)
  682. {
  683. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  684. /* Disable interrupts */
  685. atmel_hlcdc_layer_write_reg(&plane->layer, ATMEL_HLCDC_LAYER_IDR,
  686. 0xffffffff);
  687. /* Disable the layer */
  688. atmel_hlcdc_layer_write_reg(&plane->layer, ATMEL_HLCDC_LAYER_CHDR,
  689. ATMEL_HLCDC_LAYER_RST |
  690. ATMEL_HLCDC_LAYER_A2Q |
  691. ATMEL_HLCDC_LAYER_UPDATE);
  692. /* Clear all pending interrupts */
  693. atmel_hlcdc_layer_read_reg(&plane->layer, ATMEL_HLCDC_LAYER_ISR);
  694. }
  695. static int atmel_hlcdc_plane_init_properties(struct atmel_hlcdc_plane *plane)
  696. {
  697. const struct atmel_hlcdc_layer_desc *desc = plane->layer.desc;
  698. if (desc->type == ATMEL_HLCDC_OVERLAY_LAYER ||
  699. desc->type == ATMEL_HLCDC_CURSOR_LAYER) {
  700. int ret;
  701. ret = drm_plane_create_alpha_property(&plane->base);
  702. if (ret)
  703. return ret;
  704. }
  705. if (desc->layout.xstride[0] && desc->layout.pstride[0]) {
  706. int ret;
  707. ret = drm_plane_create_rotation_property(&plane->base,
  708. DRM_MODE_ROTATE_0,
  709. DRM_MODE_ROTATE_0 |
  710. DRM_MODE_ROTATE_90 |
  711. DRM_MODE_ROTATE_180 |
  712. DRM_MODE_ROTATE_270);
  713. if (ret)
  714. return ret;
  715. }
  716. if (desc->layout.csc) {
  717. /*
  718. * TODO: decare a "yuv-to-rgb-conv-factors" property to let
  719. * userspace modify these factors (using a BLOB property ?).
  720. */
  721. atmel_hlcdc_layer_write_cfg(&plane->layer,
  722. desc->layout.csc,
  723. 0x4c900091);
  724. atmel_hlcdc_layer_write_cfg(&plane->layer,
  725. desc->layout.csc + 1,
  726. 0x7a5f5090);
  727. atmel_hlcdc_layer_write_cfg(&plane->layer,
  728. desc->layout.csc + 2,
  729. 0x40040890);
  730. }
  731. return 0;
  732. }
  733. void atmel_hlcdc_plane_irq(struct atmel_hlcdc_plane *plane)
  734. {
  735. const struct atmel_hlcdc_layer_desc *desc = plane->layer.desc;
  736. u32 isr;
  737. isr = atmel_hlcdc_layer_read_reg(&plane->layer, ATMEL_HLCDC_LAYER_ISR);
  738. /*
  739. * There's not much we can do in case of overrun except informing
  740. * the user. However, we are in interrupt context here, hence the
  741. * use of dev_dbg().
  742. */
  743. if (isr &
  744. (ATMEL_HLCDC_LAYER_OVR_IRQ(0) | ATMEL_HLCDC_LAYER_OVR_IRQ(1) |
  745. ATMEL_HLCDC_LAYER_OVR_IRQ(2)))
  746. dev_dbg(plane->base.dev->dev, "overrun on plane %s\n",
  747. desc->name);
  748. }
  749. static const struct drm_plane_helper_funcs atmel_hlcdc_layer_plane_helper_funcs = {
  750. .atomic_check = atmel_hlcdc_plane_atomic_check,
  751. .atomic_update = atmel_hlcdc_plane_atomic_update,
  752. .atomic_disable = atmel_hlcdc_plane_atomic_disable,
  753. };
  754. static int atmel_hlcdc_plane_alloc_dscrs(struct drm_plane *p,
  755. struct atmel_hlcdc_plane_state *state)
  756. {
  757. struct atmel_hlcdc_dc *dc = p->dev->dev_private;
  758. int i;
  759. for (i = 0; i < ARRAY_SIZE(state->dscrs); i++) {
  760. struct atmel_hlcdc_dma_channel_dscr *dscr;
  761. dma_addr_t dscr_dma;
  762. dscr = dma_pool_alloc(dc->dscrpool, GFP_KERNEL, &dscr_dma);
  763. if (!dscr)
  764. goto err;
  765. dscr->addr = 0;
  766. dscr->next = dscr_dma;
  767. dscr->self = dscr_dma;
  768. dscr->ctrl = ATMEL_HLCDC_LAYER_DFETCH;
  769. state->dscrs[i] = dscr;
  770. }
  771. return 0;
  772. err:
  773. for (i--; i >= 0; i--) {
  774. dma_pool_free(dc->dscrpool, state->dscrs[i],
  775. state->dscrs[i]->self);
  776. }
  777. return -ENOMEM;
  778. }
  779. static void atmel_hlcdc_plane_reset(struct drm_plane *p)
  780. {
  781. struct atmel_hlcdc_plane_state *state;
  782. if (p->state) {
  783. state = drm_plane_state_to_atmel_hlcdc_plane_state(p->state);
  784. if (state->base.fb)
  785. drm_framebuffer_put(state->base.fb);
  786. kfree(state);
  787. p->state = NULL;
  788. }
  789. state = kzalloc(sizeof(*state), GFP_KERNEL);
  790. if (state) {
  791. if (atmel_hlcdc_plane_alloc_dscrs(p, state)) {
  792. kfree(state);
  793. dev_err(p->dev->dev,
  794. "Failed to allocate initial plane state\n");
  795. return;
  796. }
  797. __drm_atomic_helper_plane_reset(p, &state->base);
  798. }
  799. }
  800. static struct drm_plane_state *
  801. atmel_hlcdc_plane_atomic_duplicate_state(struct drm_plane *p)
  802. {
  803. struct atmel_hlcdc_plane_state *state =
  804. drm_plane_state_to_atmel_hlcdc_plane_state(p->state);
  805. struct atmel_hlcdc_plane_state *copy;
  806. copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
  807. if (!copy)
  808. return NULL;
  809. if (atmel_hlcdc_plane_alloc_dscrs(p, copy)) {
  810. kfree(copy);
  811. return NULL;
  812. }
  813. if (copy->base.fb)
  814. drm_framebuffer_get(copy->base.fb);
  815. return &copy->base;
  816. }
  817. static void atmel_hlcdc_plane_atomic_destroy_state(struct drm_plane *p,
  818. struct drm_plane_state *s)
  819. {
  820. struct atmel_hlcdc_plane_state *state =
  821. drm_plane_state_to_atmel_hlcdc_plane_state(s);
  822. struct atmel_hlcdc_dc *dc = p->dev->dev_private;
  823. int i;
  824. for (i = 0; i < ARRAY_SIZE(state->dscrs); i++) {
  825. dma_pool_free(dc->dscrpool, state->dscrs[i],
  826. state->dscrs[i]->self);
  827. }
  828. if (s->fb)
  829. drm_framebuffer_put(s->fb);
  830. kfree(state);
  831. }
  832. static const struct drm_plane_funcs layer_plane_funcs = {
  833. .update_plane = drm_atomic_helper_update_plane,
  834. .disable_plane = drm_atomic_helper_disable_plane,
  835. .destroy = drm_plane_cleanup,
  836. .reset = atmel_hlcdc_plane_reset,
  837. .atomic_duplicate_state = atmel_hlcdc_plane_atomic_duplicate_state,
  838. .atomic_destroy_state = atmel_hlcdc_plane_atomic_destroy_state,
  839. };
  840. static int atmel_hlcdc_plane_create(struct drm_device *dev,
  841. const struct atmel_hlcdc_layer_desc *desc)
  842. {
  843. struct atmel_hlcdc_dc *dc = dev->dev_private;
  844. struct atmel_hlcdc_plane *plane;
  845. enum drm_plane_type type;
  846. int ret;
  847. plane = devm_kzalloc(dev->dev, sizeof(*plane), GFP_KERNEL);
  848. if (!plane)
  849. return -ENOMEM;
  850. atmel_hlcdc_layer_init(&plane->layer, desc, dc->hlcdc->regmap);
  851. if (desc->type == ATMEL_HLCDC_BASE_LAYER)
  852. type = DRM_PLANE_TYPE_PRIMARY;
  853. else if (desc->type == ATMEL_HLCDC_CURSOR_LAYER)
  854. type = DRM_PLANE_TYPE_CURSOR;
  855. else
  856. type = DRM_PLANE_TYPE_OVERLAY;
  857. ret = drm_universal_plane_init(dev, &plane->base, 0,
  858. &layer_plane_funcs,
  859. desc->formats->formats,
  860. desc->formats->nformats,
  861. NULL, type, NULL);
  862. if (ret)
  863. return ret;
  864. drm_plane_helper_add(&plane->base,
  865. &atmel_hlcdc_layer_plane_helper_funcs);
  866. /* Set default property values*/
  867. ret = atmel_hlcdc_plane_init_properties(plane);
  868. if (ret)
  869. return ret;
  870. dc->layers[desc->id] = &plane->layer;
  871. return 0;
  872. }
  873. int atmel_hlcdc_create_planes(struct drm_device *dev)
  874. {
  875. struct atmel_hlcdc_dc *dc = dev->dev_private;
  876. const struct atmel_hlcdc_layer_desc *descs = dc->desc->layers;
  877. int nlayers = dc->desc->nlayers;
  878. int i, ret;
  879. dc->dscrpool = dmam_pool_create("atmel-hlcdc-dscr", dev->dev,
  880. sizeof(struct atmel_hlcdc_dma_channel_dscr),
  881. sizeof(u64), 0);
  882. if (!dc->dscrpool)
  883. return -ENOMEM;
  884. for (i = 0; i < nlayers; i++) {
  885. if (descs[i].type != ATMEL_HLCDC_BASE_LAYER &&
  886. descs[i].type != ATMEL_HLCDC_OVERLAY_LAYER &&
  887. descs[i].type != ATMEL_HLCDC_CURSOR_LAYER)
  888. continue;
  889. ret = atmel_hlcdc_plane_create(dev, &descs[i]);
  890. if (ret)
  891. return ret;
  892. }
  893. return 0;
  894. }