atmel_hlcdc_plane.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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. {
  366. struct drm_crtc *crtc = plane->base.crtc;
  367. struct drm_color_lut *lut;
  368. int idx;
  369. if (!crtc || !crtc->state)
  370. return;
  371. if (!crtc->state->color_mgmt_changed || !crtc->state->gamma_lut)
  372. return;
  373. lut = (struct drm_color_lut *)crtc->state->gamma_lut->data;
  374. for (idx = 0; idx < ATMEL_HLCDC_CLUT_SIZE; idx++, lut++) {
  375. u32 val = ((lut->red << 8) & 0xff0000) |
  376. (lut->green & 0xff00) |
  377. (lut->blue >> 8);
  378. atmel_hlcdc_layer_write_clut(&plane->layer, idx, val);
  379. }
  380. }
  381. static void atmel_hlcdc_plane_update_buffers(struct atmel_hlcdc_plane *plane,
  382. struct atmel_hlcdc_plane_state *state)
  383. {
  384. const struct atmel_hlcdc_layer_desc *desc = plane->layer.desc;
  385. struct drm_framebuffer *fb = state->base.fb;
  386. u32 sr;
  387. int i;
  388. sr = atmel_hlcdc_layer_read_reg(&plane->layer, ATMEL_HLCDC_LAYER_CHSR);
  389. for (i = 0; i < state->nplanes; i++) {
  390. struct drm_gem_cma_object *gem = drm_fb_cma_get_gem_obj(fb, i);
  391. state->dscrs[i]->addr = gem->paddr + state->offsets[i];
  392. atmel_hlcdc_layer_write_reg(&plane->layer,
  393. ATMEL_HLCDC_LAYER_PLANE_HEAD(i),
  394. state->dscrs[i]->self);
  395. if (!(sr & ATMEL_HLCDC_LAYER_EN)) {
  396. atmel_hlcdc_layer_write_reg(&plane->layer,
  397. ATMEL_HLCDC_LAYER_PLANE_ADDR(i),
  398. state->dscrs[i]->addr);
  399. atmel_hlcdc_layer_write_reg(&plane->layer,
  400. ATMEL_HLCDC_LAYER_PLANE_CTRL(i),
  401. state->dscrs[i]->ctrl);
  402. atmel_hlcdc_layer_write_reg(&plane->layer,
  403. ATMEL_HLCDC_LAYER_PLANE_NEXT(i),
  404. state->dscrs[i]->self);
  405. }
  406. if (desc->layout.xstride[i])
  407. atmel_hlcdc_layer_write_cfg(&plane->layer,
  408. desc->layout.xstride[i],
  409. state->xstride[i]);
  410. if (desc->layout.pstride[i])
  411. atmel_hlcdc_layer_write_cfg(&plane->layer,
  412. desc->layout.pstride[i],
  413. state->pstride[i]);
  414. }
  415. }
  416. int atmel_hlcdc_plane_prepare_ahb_routing(struct drm_crtc_state *c_state)
  417. {
  418. unsigned int ahb_load[2] = { };
  419. struct drm_plane *plane;
  420. drm_atomic_crtc_state_for_each_plane(plane, c_state) {
  421. struct atmel_hlcdc_plane_state *plane_state;
  422. struct drm_plane_state *plane_s;
  423. unsigned int pixels, load = 0;
  424. int i;
  425. plane_s = drm_atomic_get_plane_state(c_state->state, plane);
  426. if (IS_ERR(plane_s))
  427. return PTR_ERR(plane_s);
  428. plane_state =
  429. drm_plane_state_to_atmel_hlcdc_plane_state(plane_s);
  430. pixels = (plane_state->src_w * plane_state->src_h) -
  431. (plane_state->disc_w * plane_state->disc_h);
  432. for (i = 0; i < plane_state->nplanes; i++)
  433. load += pixels * plane_state->bpp[i];
  434. if (ahb_load[0] <= ahb_load[1])
  435. plane_state->ahb_id = 0;
  436. else
  437. plane_state->ahb_id = 1;
  438. ahb_load[plane_state->ahb_id] += load;
  439. }
  440. return 0;
  441. }
  442. int
  443. atmel_hlcdc_plane_prepare_disc_area(struct drm_crtc_state *c_state)
  444. {
  445. int disc_x = 0, disc_y = 0, disc_w = 0, disc_h = 0;
  446. const struct atmel_hlcdc_layer_cfg_layout *layout;
  447. struct atmel_hlcdc_plane_state *primary_state;
  448. struct drm_plane_state *primary_s;
  449. struct atmel_hlcdc_plane *primary;
  450. struct drm_plane *ovl;
  451. primary = drm_plane_to_atmel_hlcdc_plane(c_state->crtc->primary);
  452. layout = &primary->layer.desc->layout;
  453. if (!layout->disc_pos || !layout->disc_size)
  454. return 0;
  455. primary_s = drm_atomic_get_plane_state(c_state->state,
  456. &primary->base);
  457. if (IS_ERR(primary_s))
  458. return PTR_ERR(primary_s);
  459. primary_state = drm_plane_state_to_atmel_hlcdc_plane_state(primary_s);
  460. drm_atomic_crtc_state_for_each_plane(ovl, c_state) {
  461. struct atmel_hlcdc_plane_state *ovl_state;
  462. struct drm_plane_state *ovl_s;
  463. if (ovl == c_state->crtc->primary)
  464. continue;
  465. ovl_s = drm_atomic_get_plane_state(c_state->state, ovl);
  466. if (IS_ERR(ovl_s))
  467. return PTR_ERR(ovl_s);
  468. ovl_state = drm_plane_state_to_atmel_hlcdc_plane_state(ovl_s);
  469. if (!ovl_s->fb ||
  470. ovl_s->fb->format->has_alpha ||
  471. ovl_s->alpha != DRM_BLEND_ALPHA_OPAQUE)
  472. continue;
  473. /* TODO: implement a smarter hidden area detection */
  474. if (ovl_state->crtc_h * ovl_state->crtc_w < disc_h * disc_w)
  475. continue;
  476. disc_x = ovl_state->crtc_x;
  477. disc_y = ovl_state->crtc_y;
  478. disc_h = ovl_state->crtc_h;
  479. disc_w = ovl_state->crtc_w;
  480. }
  481. primary_state->disc_x = disc_x;
  482. primary_state->disc_y = disc_y;
  483. primary_state->disc_w = disc_w;
  484. primary_state->disc_h = disc_h;
  485. return 0;
  486. }
  487. static void
  488. atmel_hlcdc_plane_update_disc_area(struct atmel_hlcdc_plane *plane,
  489. struct atmel_hlcdc_plane_state *state)
  490. {
  491. const struct atmel_hlcdc_layer_cfg_layout *layout;
  492. layout = &plane->layer.desc->layout;
  493. if (!layout->disc_pos || !layout->disc_size)
  494. return;
  495. atmel_hlcdc_layer_write_cfg(&plane->layer, layout->disc_pos,
  496. ATMEL_HLCDC_LAYER_DISC_POS(state->disc_x,
  497. state->disc_y));
  498. atmel_hlcdc_layer_write_cfg(&plane->layer, layout->disc_size,
  499. ATMEL_HLCDC_LAYER_DISC_SIZE(state->disc_w,
  500. state->disc_h));
  501. }
  502. static int atmel_hlcdc_plane_atomic_check(struct drm_plane *p,
  503. struct drm_plane_state *s)
  504. {
  505. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  506. struct atmel_hlcdc_plane_state *state =
  507. drm_plane_state_to_atmel_hlcdc_plane_state(s);
  508. const struct atmel_hlcdc_layer_desc *desc = plane->layer.desc;
  509. struct drm_framebuffer *fb = state->base.fb;
  510. const struct drm_display_mode *mode;
  511. struct drm_crtc_state *crtc_state;
  512. unsigned int patched_crtc_w;
  513. unsigned int patched_crtc_h;
  514. unsigned int patched_src_w;
  515. unsigned int patched_src_h;
  516. unsigned int tmp;
  517. int x_offset = 0;
  518. int y_offset = 0;
  519. int hsub = 1;
  520. int vsub = 1;
  521. int i;
  522. if (!state->base.crtc || !fb)
  523. return 0;
  524. crtc_state = drm_atomic_get_existing_crtc_state(s->state, s->crtc);
  525. mode = &crtc_state->adjusted_mode;
  526. state->src_x = s->src_x;
  527. state->src_y = s->src_y;
  528. state->src_h = s->src_h;
  529. state->src_w = s->src_w;
  530. state->crtc_x = s->crtc_x;
  531. state->crtc_y = s->crtc_y;
  532. state->crtc_h = s->crtc_h;
  533. state->crtc_w = s->crtc_w;
  534. if ((state->src_x | state->src_y | state->src_w | state->src_h) &
  535. SUBPIXEL_MASK)
  536. return -EINVAL;
  537. state->src_x >>= 16;
  538. state->src_y >>= 16;
  539. state->src_w >>= 16;
  540. state->src_h >>= 16;
  541. state->nplanes = fb->format->num_planes;
  542. if (state->nplanes > ATMEL_HLCDC_LAYER_MAX_PLANES)
  543. return -EINVAL;
  544. /*
  545. * Swap width and size in case of 90 or 270 degrees rotation
  546. */
  547. if (drm_rotation_90_or_270(state->base.rotation)) {
  548. tmp = state->crtc_w;
  549. state->crtc_w = state->crtc_h;
  550. state->crtc_h = tmp;
  551. tmp = state->src_w;
  552. state->src_w = state->src_h;
  553. state->src_h = tmp;
  554. }
  555. if (state->crtc_x + state->crtc_w > mode->hdisplay)
  556. patched_crtc_w = mode->hdisplay - state->crtc_x;
  557. else
  558. patched_crtc_w = state->crtc_w;
  559. if (state->crtc_x < 0) {
  560. patched_crtc_w += state->crtc_x;
  561. x_offset = -state->crtc_x;
  562. state->crtc_x = 0;
  563. }
  564. if (state->crtc_y + state->crtc_h > mode->vdisplay)
  565. patched_crtc_h = mode->vdisplay - state->crtc_y;
  566. else
  567. patched_crtc_h = state->crtc_h;
  568. if (state->crtc_y < 0) {
  569. patched_crtc_h += state->crtc_y;
  570. y_offset = -state->crtc_y;
  571. state->crtc_y = 0;
  572. }
  573. patched_src_w = DIV_ROUND_CLOSEST(patched_crtc_w * state->src_w,
  574. state->crtc_w);
  575. patched_src_h = DIV_ROUND_CLOSEST(patched_crtc_h * state->src_h,
  576. state->crtc_h);
  577. hsub = drm_format_horz_chroma_subsampling(fb->format->format);
  578. vsub = drm_format_vert_chroma_subsampling(fb->format->format);
  579. for (i = 0; i < state->nplanes; i++) {
  580. unsigned int offset = 0;
  581. int xdiv = i ? hsub : 1;
  582. int ydiv = i ? vsub : 1;
  583. state->bpp[i] = fb->format->cpp[i];
  584. if (!state->bpp[i])
  585. return -EINVAL;
  586. switch (state->base.rotation & DRM_MODE_ROTATE_MASK) {
  587. case DRM_MODE_ROTATE_90:
  588. offset = ((y_offset + state->src_y + patched_src_w - 1) /
  589. ydiv) * fb->pitches[i];
  590. offset += ((x_offset + state->src_x) / xdiv) *
  591. state->bpp[i];
  592. state->xstride[i] = ((patched_src_w - 1) / ydiv) *
  593. fb->pitches[i];
  594. state->pstride[i] = -fb->pitches[i] - state->bpp[i];
  595. break;
  596. case DRM_MODE_ROTATE_180:
  597. offset = ((y_offset + state->src_y + patched_src_h - 1) /
  598. ydiv) * fb->pitches[i];
  599. offset += ((x_offset + state->src_x + patched_src_w - 1) /
  600. xdiv) * state->bpp[i];
  601. state->xstride[i] = ((((patched_src_w - 1) / xdiv) - 1) *
  602. state->bpp[i]) - fb->pitches[i];
  603. state->pstride[i] = -2 * state->bpp[i];
  604. break;
  605. case DRM_MODE_ROTATE_270:
  606. offset = ((y_offset + state->src_y) / ydiv) *
  607. fb->pitches[i];
  608. offset += ((x_offset + state->src_x + patched_src_h - 1) /
  609. xdiv) * state->bpp[i];
  610. state->xstride[i] = -(((patched_src_w - 1) / ydiv) *
  611. fb->pitches[i]) -
  612. (2 * state->bpp[i]);
  613. state->pstride[i] = fb->pitches[i] - state->bpp[i];
  614. break;
  615. case DRM_MODE_ROTATE_0:
  616. default:
  617. offset = ((y_offset + state->src_y) / ydiv) *
  618. fb->pitches[i];
  619. offset += ((x_offset + state->src_x) / xdiv) *
  620. state->bpp[i];
  621. state->xstride[i] = fb->pitches[i] -
  622. ((patched_src_w / xdiv) *
  623. state->bpp[i]);
  624. state->pstride[i] = 0;
  625. break;
  626. }
  627. state->offsets[i] = offset + fb->offsets[i];
  628. }
  629. state->src_w = patched_src_w;
  630. state->src_h = patched_src_h;
  631. state->crtc_w = patched_crtc_w;
  632. state->crtc_h = patched_crtc_h;
  633. if (!desc->layout.size &&
  634. (mode->hdisplay != state->crtc_w ||
  635. mode->vdisplay != state->crtc_h))
  636. return -EINVAL;
  637. if (desc->max_height && state->crtc_h > desc->max_height)
  638. return -EINVAL;
  639. if (desc->max_width && state->crtc_w > desc->max_width)
  640. return -EINVAL;
  641. if ((state->crtc_h != state->src_h || state->crtc_w != state->src_w) &&
  642. (!desc->layout.memsize ||
  643. state->base.fb->format->has_alpha))
  644. return -EINVAL;
  645. if (state->crtc_x < 0 || state->crtc_y < 0)
  646. return -EINVAL;
  647. if (state->crtc_w + state->crtc_x > mode->hdisplay ||
  648. state->crtc_h + state->crtc_y > mode->vdisplay)
  649. return -EINVAL;
  650. return 0;
  651. }
  652. static void atmel_hlcdc_plane_atomic_update(struct drm_plane *p,
  653. struct drm_plane_state *old_s)
  654. {
  655. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  656. struct atmel_hlcdc_plane_state *state =
  657. drm_plane_state_to_atmel_hlcdc_plane_state(p->state);
  658. u32 sr;
  659. if (!p->state->crtc || !p->state->fb)
  660. return;
  661. atmel_hlcdc_plane_update_pos_and_size(plane, state);
  662. atmel_hlcdc_plane_update_general_settings(plane, state);
  663. atmel_hlcdc_plane_update_format(plane, state);
  664. atmel_hlcdc_plane_update_clut(plane);
  665. atmel_hlcdc_plane_update_buffers(plane, state);
  666. atmel_hlcdc_plane_update_disc_area(plane, state);
  667. /* Enable the overrun interrupts. */
  668. atmel_hlcdc_layer_write_reg(&plane->layer, ATMEL_HLCDC_LAYER_IER,
  669. ATMEL_HLCDC_LAYER_OVR_IRQ(0) |
  670. ATMEL_HLCDC_LAYER_OVR_IRQ(1) |
  671. ATMEL_HLCDC_LAYER_OVR_IRQ(2));
  672. /* Apply the new config at the next SOF event. */
  673. sr = atmel_hlcdc_layer_read_reg(&plane->layer, ATMEL_HLCDC_LAYER_CHSR);
  674. atmel_hlcdc_layer_write_reg(&plane->layer, ATMEL_HLCDC_LAYER_CHER,
  675. ATMEL_HLCDC_LAYER_UPDATE |
  676. (sr & ATMEL_HLCDC_LAYER_EN ?
  677. ATMEL_HLCDC_LAYER_A2Q : ATMEL_HLCDC_LAYER_EN));
  678. }
  679. static void atmel_hlcdc_plane_atomic_disable(struct drm_plane *p,
  680. struct drm_plane_state *old_state)
  681. {
  682. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  683. /* Disable interrupts */
  684. atmel_hlcdc_layer_write_reg(&plane->layer, ATMEL_HLCDC_LAYER_IDR,
  685. 0xffffffff);
  686. /* Disable the layer */
  687. atmel_hlcdc_layer_write_reg(&plane->layer, ATMEL_HLCDC_LAYER_CHDR,
  688. ATMEL_HLCDC_LAYER_RST |
  689. ATMEL_HLCDC_LAYER_A2Q |
  690. ATMEL_HLCDC_LAYER_UPDATE);
  691. /* Clear all pending interrupts */
  692. atmel_hlcdc_layer_read_reg(&plane->layer, ATMEL_HLCDC_LAYER_ISR);
  693. }
  694. static void atmel_hlcdc_plane_destroy(struct drm_plane *p)
  695. {
  696. struct atmel_hlcdc_plane *plane = drm_plane_to_atmel_hlcdc_plane(p);
  697. if (plane->base.fb)
  698. drm_framebuffer_put(plane->base.fb);
  699. drm_plane_cleanup(p);
  700. }
  701. static int atmel_hlcdc_plane_init_properties(struct atmel_hlcdc_plane *plane)
  702. {
  703. const struct atmel_hlcdc_layer_desc *desc = plane->layer.desc;
  704. if (desc->type == ATMEL_HLCDC_OVERLAY_LAYER ||
  705. desc->type == ATMEL_HLCDC_CURSOR_LAYER) {
  706. int ret;
  707. ret = drm_plane_create_alpha_property(&plane->base);
  708. if (ret)
  709. return ret;
  710. }
  711. if (desc->layout.xstride && desc->layout.pstride) {
  712. int ret;
  713. ret = drm_plane_create_rotation_property(&plane->base,
  714. DRM_MODE_ROTATE_0,
  715. DRM_MODE_ROTATE_0 |
  716. DRM_MODE_ROTATE_90 |
  717. DRM_MODE_ROTATE_180 |
  718. DRM_MODE_ROTATE_270);
  719. if (ret)
  720. return ret;
  721. }
  722. if (desc->layout.csc) {
  723. /*
  724. * TODO: decare a "yuv-to-rgb-conv-factors" property to let
  725. * userspace modify these factors (using a BLOB property ?).
  726. */
  727. atmel_hlcdc_layer_write_cfg(&plane->layer,
  728. desc->layout.csc,
  729. 0x4c900091);
  730. atmel_hlcdc_layer_write_cfg(&plane->layer,
  731. desc->layout.csc + 1,
  732. 0x7a5f5090);
  733. atmel_hlcdc_layer_write_cfg(&plane->layer,
  734. desc->layout.csc + 2,
  735. 0x40040890);
  736. }
  737. return 0;
  738. }
  739. void atmel_hlcdc_plane_irq(struct atmel_hlcdc_plane *plane)
  740. {
  741. const struct atmel_hlcdc_layer_desc *desc = plane->layer.desc;
  742. u32 isr;
  743. isr = atmel_hlcdc_layer_read_reg(&plane->layer, ATMEL_HLCDC_LAYER_ISR);
  744. /*
  745. * There's not much we can do in case of overrun except informing
  746. * the user. However, we are in interrupt context here, hence the
  747. * use of dev_dbg().
  748. */
  749. if (isr &
  750. (ATMEL_HLCDC_LAYER_OVR_IRQ(0) | ATMEL_HLCDC_LAYER_OVR_IRQ(1) |
  751. ATMEL_HLCDC_LAYER_OVR_IRQ(2)))
  752. dev_dbg(plane->base.dev->dev, "overrun on plane %s\n",
  753. desc->name);
  754. }
  755. static const struct drm_plane_helper_funcs atmel_hlcdc_layer_plane_helper_funcs = {
  756. .atomic_check = atmel_hlcdc_plane_atomic_check,
  757. .atomic_update = atmel_hlcdc_plane_atomic_update,
  758. .atomic_disable = atmel_hlcdc_plane_atomic_disable,
  759. };
  760. static int atmel_hlcdc_plane_alloc_dscrs(struct drm_plane *p,
  761. struct atmel_hlcdc_plane_state *state)
  762. {
  763. struct atmel_hlcdc_dc *dc = p->dev->dev_private;
  764. int i;
  765. for (i = 0; i < ARRAY_SIZE(state->dscrs); i++) {
  766. struct atmel_hlcdc_dma_channel_dscr *dscr;
  767. dma_addr_t dscr_dma;
  768. dscr = dma_pool_alloc(dc->dscrpool, GFP_KERNEL, &dscr_dma);
  769. if (!dscr)
  770. goto err;
  771. dscr->addr = 0;
  772. dscr->next = dscr_dma;
  773. dscr->self = dscr_dma;
  774. dscr->ctrl = ATMEL_HLCDC_LAYER_DFETCH;
  775. state->dscrs[i] = dscr;
  776. }
  777. return 0;
  778. err:
  779. for (i--; i >= 0; i--) {
  780. dma_pool_free(dc->dscrpool, state->dscrs[i],
  781. state->dscrs[i]->self);
  782. }
  783. return -ENOMEM;
  784. }
  785. static void atmel_hlcdc_plane_reset(struct drm_plane *p)
  786. {
  787. struct atmel_hlcdc_plane_state *state;
  788. if (p->state) {
  789. state = drm_plane_state_to_atmel_hlcdc_plane_state(p->state);
  790. if (state->base.fb)
  791. drm_framebuffer_put(state->base.fb);
  792. kfree(state);
  793. p->state = NULL;
  794. }
  795. state = kzalloc(sizeof(*state), GFP_KERNEL);
  796. if (state) {
  797. if (atmel_hlcdc_plane_alloc_dscrs(p, state)) {
  798. kfree(state);
  799. dev_err(p->dev->dev,
  800. "Failed to allocate initial plane state\n");
  801. return;
  802. }
  803. p->state = &state->base;
  804. p->state->alpha = DRM_BLEND_ALPHA_OPAQUE;
  805. p->state->plane = p;
  806. }
  807. }
  808. static struct drm_plane_state *
  809. atmel_hlcdc_plane_atomic_duplicate_state(struct drm_plane *p)
  810. {
  811. struct atmel_hlcdc_plane_state *state =
  812. drm_plane_state_to_atmel_hlcdc_plane_state(p->state);
  813. struct atmel_hlcdc_plane_state *copy;
  814. copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
  815. if (!copy)
  816. return NULL;
  817. if (atmel_hlcdc_plane_alloc_dscrs(p, copy)) {
  818. kfree(copy);
  819. return NULL;
  820. }
  821. if (copy->base.fb)
  822. drm_framebuffer_get(copy->base.fb);
  823. return &copy->base;
  824. }
  825. static void atmel_hlcdc_plane_atomic_destroy_state(struct drm_plane *p,
  826. struct drm_plane_state *s)
  827. {
  828. struct atmel_hlcdc_plane_state *state =
  829. drm_plane_state_to_atmel_hlcdc_plane_state(s);
  830. struct atmel_hlcdc_dc *dc = p->dev->dev_private;
  831. int i;
  832. for (i = 0; i < ARRAY_SIZE(state->dscrs); i++) {
  833. dma_pool_free(dc->dscrpool, state->dscrs[i],
  834. state->dscrs[i]->self);
  835. }
  836. if (s->fb)
  837. drm_framebuffer_put(s->fb);
  838. kfree(state);
  839. }
  840. static const struct drm_plane_funcs layer_plane_funcs = {
  841. .update_plane = drm_atomic_helper_update_plane,
  842. .disable_plane = drm_atomic_helper_disable_plane,
  843. .destroy = atmel_hlcdc_plane_destroy,
  844. .reset = atmel_hlcdc_plane_reset,
  845. .atomic_duplicate_state = atmel_hlcdc_plane_atomic_duplicate_state,
  846. .atomic_destroy_state = atmel_hlcdc_plane_atomic_destroy_state,
  847. };
  848. static int atmel_hlcdc_plane_create(struct drm_device *dev,
  849. const struct atmel_hlcdc_layer_desc *desc)
  850. {
  851. struct atmel_hlcdc_dc *dc = dev->dev_private;
  852. struct atmel_hlcdc_plane *plane;
  853. enum drm_plane_type type;
  854. int ret;
  855. plane = devm_kzalloc(dev->dev, sizeof(*plane), GFP_KERNEL);
  856. if (!plane)
  857. return -ENOMEM;
  858. atmel_hlcdc_layer_init(&plane->layer, desc, dc->hlcdc->regmap);
  859. if (desc->type == ATMEL_HLCDC_BASE_LAYER)
  860. type = DRM_PLANE_TYPE_PRIMARY;
  861. else if (desc->type == ATMEL_HLCDC_CURSOR_LAYER)
  862. type = DRM_PLANE_TYPE_CURSOR;
  863. else
  864. type = DRM_PLANE_TYPE_OVERLAY;
  865. ret = drm_universal_plane_init(dev, &plane->base, 0,
  866. &layer_plane_funcs,
  867. desc->formats->formats,
  868. desc->formats->nformats,
  869. NULL, type, NULL);
  870. if (ret)
  871. return ret;
  872. drm_plane_helper_add(&plane->base,
  873. &atmel_hlcdc_layer_plane_helper_funcs);
  874. /* Set default property values*/
  875. ret = atmel_hlcdc_plane_init_properties(plane);
  876. if (ret)
  877. return ret;
  878. dc->layers[desc->id] = &plane->layer;
  879. return 0;
  880. }
  881. int atmel_hlcdc_create_planes(struct drm_device *dev)
  882. {
  883. struct atmel_hlcdc_dc *dc = dev->dev_private;
  884. const struct atmel_hlcdc_layer_desc *descs = dc->desc->layers;
  885. int nlayers = dc->desc->nlayers;
  886. int i, ret;
  887. dc->dscrpool = dmam_pool_create("atmel-hlcdc-dscr", dev->dev,
  888. sizeof(struct atmel_hlcdc_dma_channel_dscr),
  889. sizeof(u64), 0);
  890. if (!dc->dscrpool)
  891. return -ENOMEM;
  892. for (i = 0; i < nlayers; i++) {
  893. if (descs[i].type != ATMEL_HLCDC_BASE_LAYER &&
  894. descs[i].type != ATMEL_HLCDC_OVERLAY_LAYER &&
  895. descs[i].type != ATMEL_HLCDC_CURSOR_LAYER)
  896. continue;
  897. ret = atmel_hlcdc_plane_create(dev, &descs[i]);
  898. if (ret)
  899. return ret;
  900. }
  901. return 0;
  902. }