atmel_hlcdc_plane.c 29 KB

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