atmel_hlcdc_plane.c 29 KB

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