atmel_hlcdc_plane.c 29 KB

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