intel_sprite.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663
  1. /*
  2. * Copyright © 2011 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. *
  23. * Authors:
  24. * Jesse Barnes <jbarnes@virtuousgeek.org>
  25. *
  26. * New plane/sprite handling.
  27. *
  28. * The older chips had a separate interface for programming plane related
  29. * registers; newer ones are much simpler and we can use the new DRM plane
  30. * support.
  31. */
  32. #include <drm/drmP.h>
  33. #include <drm/drm_crtc.h>
  34. #include <drm/drm_fourcc.h>
  35. #include <drm/drm_rect.h>
  36. #include "intel_drv.h"
  37. #include <drm/i915_drm.h>
  38. #include "i915_drv.h"
  39. static int usecs_to_scanlines(const struct drm_display_mode *mode, int usecs)
  40. {
  41. /* paranoia */
  42. if (!mode->crtc_htotal)
  43. return 1;
  44. return DIV_ROUND_UP(usecs * mode->crtc_clock, 1000 * mode->crtc_htotal);
  45. }
  46. static bool intel_pipe_update_start(struct intel_crtc *crtc, uint32_t *start_vbl_count)
  47. {
  48. struct drm_device *dev = crtc->base.dev;
  49. const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
  50. enum pipe pipe = crtc->pipe;
  51. long timeout = msecs_to_jiffies_timeout(1);
  52. int scanline, min, max, vblank_start;
  53. wait_queue_head_t *wq = drm_crtc_vblank_waitqueue(&crtc->base);
  54. DEFINE_WAIT(wait);
  55. WARN_ON(!drm_modeset_is_locked(&crtc->base.mutex));
  56. vblank_start = mode->crtc_vblank_start;
  57. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  58. vblank_start = DIV_ROUND_UP(vblank_start, 2);
  59. /* FIXME needs to be calibrated sensibly */
  60. min = vblank_start - usecs_to_scanlines(mode, 100);
  61. max = vblank_start - 1;
  62. if (min <= 0 || max <= 0)
  63. return false;
  64. if (WARN_ON(drm_vblank_get(dev, pipe)))
  65. return false;
  66. local_irq_disable();
  67. trace_i915_pipe_update_start(crtc, min, max);
  68. for (;;) {
  69. /*
  70. * prepare_to_wait() has a memory barrier, which guarantees
  71. * other CPUs can see the task state update by the time we
  72. * read the scanline.
  73. */
  74. prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
  75. scanline = intel_get_crtc_scanline(crtc);
  76. if (scanline < min || scanline > max)
  77. break;
  78. if (timeout <= 0) {
  79. DRM_ERROR("Potential atomic update failure on pipe %c\n",
  80. pipe_name(crtc->pipe));
  81. break;
  82. }
  83. local_irq_enable();
  84. timeout = schedule_timeout(timeout);
  85. local_irq_disable();
  86. }
  87. finish_wait(wq, &wait);
  88. drm_vblank_put(dev, pipe);
  89. *start_vbl_count = dev->driver->get_vblank_counter(dev, pipe);
  90. trace_i915_pipe_update_vblank_evaded(crtc, min, max, *start_vbl_count);
  91. return true;
  92. }
  93. static void intel_pipe_update_end(struct intel_crtc *crtc, u32 start_vbl_count)
  94. {
  95. struct drm_device *dev = crtc->base.dev;
  96. enum pipe pipe = crtc->pipe;
  97. u32 end_vbl_count = dev->driver->get_vblank_counter(dev, pipe);
  98. trace_i915_pipe_update_end(crtc, end_vbl_count);
  99. local_irq_enable();
  100. if (start_vbl_count != end_vbl_count)
  101. DRM_ERROR("Atomic update failure on pipe %c (start=%u end=%u)\n",
  102. pipe_name(pipe), start_vbl_count, end_vbl_count);
  103. }
  104. static void intel_update_primary_plane(struct intel_crtc *crtc)
  105. {
  106. struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
  107. int reg = DSPCNTR(crtc->plane);
  108. if (crtc->primary_enabled)
  109. I915_WRITE(reg, I915_READ(reg) | DISPLAY_PLANE_ENABLE);
  110. else
  111. I915_WRITE(reg, I915_READ(reg) & ~DISPLAY_PLANE_ENABLE);
  112. }
  113. static void
  114. skl_update_plane(struct drm_plane *drm_plane, struct drm_crtc *crtc,
  115. struct drm_framebuffer *fb,
  116. struct drm_i915_gem_object *obj, int crtc_x, int crtc_y,
  117. unsigned int crtc_w, unsigned int crtc_h,
  118. uint32_t x, uint32_t y,
  119. uint32_t src_w, uint32_t src_h)
  120. {
  121. struct drm_device *dev = drm_plane->dev;
  122. struct drm_i915_private *dev_priv = dev->dev_private;
  123. struct intel_plane *intel_plane = to_intel_plane(drm_plane);
  124. const int pipe = intel_plane->pipe;
  125. const int plane = intel_plane->plane + 1;
  126. u32 plane_ctl, stride;
  127. int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  128. plane_ctl = I915_READ(PLANE_CTL(pipe, plane));
  129. /* Mask out pixel format bits in case we change it */
  130. plane_ctl &= ~PLANE_CTL_FORMAT_MASK;
  131. plane_ctl &= ~PLANE_CTL_ORDER_RGBX;
  132. plane_ctl &= ~PLANE_CTL_YUV422_ORDER_MASK;
  133. plane_ctl &= ~PLANE_CTL_TILED_MASK;
  134. plane_ctl &= ~PLANE_CTL_ALPHA_MASK;
  135. plane_ctl &= ~PLANE_CTL_ROTATE_MASK;
  136. /* Trickle feed has to be enabled */
  137. plane_ctl &= ~PLANE_CTL_TRICKLE_FEED_DISABLE;
  138. switch (fb->pixel_format) {
  139. case DRM_FORMAT_RGB565:
  140. plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
  141. break;
  142. case DRM_FORMAT_XBGR8888:
  143. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  144. break;
  145. case DRM_FORMAT_XRGB8888:
  146. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
  147. break;
  148. /*
  149. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  150. * to be already pre-multiplied. We need to add a knob (or a different
  151. * DRM_FORMAT) for user-space to configure that.
  152. */
  153. case DRM_FORMAT_ABGR8888:
  154. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888 |
  155. PLANE_CTL_ORDER_RGBX |
  156. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  157. break;
  158. case DRM_FORMAT_ARGB8888:
  159. plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888 |
  160. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  161. break;
  162. case DRM_FORMAT_YUYV:
  163. plane_ctl |= PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  164. break;
  165. case DRM_FORMAT_YVYU:
  166. plane_ctl |= PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  167. break;
  168. case DRM_FORMAT_UYVY:
  169. plane_ctl |= PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  170. break;
  171. case DRM_FORMAT_VYUY:
  172. plane_ctl |= PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  173. break;
  174. default:
  175. BUG();
  176. }
  177. switch (obj->tiling_mode) {
  178. case I915_TILING_NONE:
  179. stride = fb->pitches[0] >> 6;
  180. break;
  181. case I915_TILING_X:
  182. plane_ctl |= PLANE_CTL_TILED_X;
  183. stride = fb->pitches[0] >> 9;
  184. break;
  185. default:
  186. BUG();
  187. }
  188. if (intel_plane->rotation == BIT(DRM_ROTATE_180))
  189. plane_ctl |= PLANE_CTL_ROTATE_180;
  190. plane_ctl |= PLANE_CTL_ENABLE;
  191. plane_ctl |= PLANE_CTL_PIPE_CSC_ENABLE;
  192. intel_update_sprite_watermarks(drm_plane, crtc, src_w, src_h,
  193. pixel_size, true,
  194. src_w != crtc_w || src_h != crtc_h);
  195. /* Sizes are 0 based */
  196. src_w--;
  197. src_h--;
  198. crtc_w--;
  199. crtc_h--;
  200. I915_WRITE(PLANE_OFFSET(pipe, plane), (y << 16) | x);
  201. I915_WRITE(PLANE_STRIDE(pipe, plane), stride);
  202. I915_WRITE(PLANE_POS(pipe, plane), (crtc_y << 16) | crtc_x);
  203. I915_WRITE(PLANE_SIZE(pipe, plane), (crtc_h << 16) | crtc_w);
  204. I915_WRITE(PLANE_CTL(pipe, plane), plane_ctl);
  205. I915_WRITE(PLANE_SURF(pipe, plane), i915_gem_obj_ggtt_offset(obj));
  206. POSTING_READ(PLANE_SURF(pipe, plane));
  207. }
  208. static void
  209. skl_disable_plane(struct drm_plane *drm_plane, struct drm_crtc *crtc)
  210. {
  211. struct drm_device *dev = drm_plane->dev;
  212. struct drm_i915_private *dev_priv = dev->dev_private;
  213. struct intel_plane *intel_plane = to_intel_plane(drm_plane);
  214. const int pipe = intel_plane->pipe;
  215. const int plane = intel_plane->plane + 1;
  216. I915_WRITE(PLANE_CTL(pipe, plane),
  217. I915_READ(PLANE_CTL(pipe, plane)) & ~PLANE_CTL_ENABLE);
  218. /* Activate double buffered register update */
  219. I915_WRITE(PLANE_CTL(pipe, plane), 0);
  220. POSTING_READ(PLANE_CTL(pipe, plane));
  221. intel_update_sprite_watermarks(drm_plane, crtc, 0, 0, 0, false, false);
  222. }
  223. static int
  224. skl_update_colorkey(struct drm_plane *drm_plane,
  225. struct drm_intel_sprite_colorkey *key)
  226. {
  227. struct drm_device *dev = drm_plane->dev;
  228. struct drm_i915_private *dev_priv = dev->dev_private;
  229. struct intel_plane *intel_plane = to_intel_plane(drm_plane);
  230. const int pipe = intel_plane->pipe;
  231. const int plane = intel_plane->plane;
  232. u32 plane_ctl;
  233. I915_WRITE(PLANE_KEYVAL(pipe, plane), key->min_value);
  234. I915_WRITE(PLANE_KEYMAX(pipe, plane), key->max_value);
  235. I915_WRITE(PLANE_KEYMSK(pipe, plane), key->channel_mask);
  236. plane_ctl = I915_READ(PLANE_CTL(pipe, plane));
  237. plane_ctl &= ~PLANE_CTL_KEY_ENABLE_MASK;
  238. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  239. plane_ctl |= PLANE_CTL_KEY_ENABLE_DESTINATION;
  240. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  241. plane_ctl |= PLANE_CTL_KEY_ENABLE_SOURCE;
  242. I915_WRITE(PLANE_CTL(pipe, plane), plane_ctl);
  243. POSTING_READ(PLANE_CTL(pipe, plane));
  244. return 0;
  245. }
  246. static void
  247. skl_get_colorkey(struct drm_plane *drm_plane,
  248. struct drm_intel_sprite_colorkey *key)
  249. {
  250. struct drm_device *dev = drm_plane->dev;
  251. struct drm_i915_private *dev_priv = dev->dev_private;
  252. struct intel_plane *intel_plane = to_intel_plane(drm_plane);
  253. const int pipe = intel_plane->pipe;
  254. const int plane = intel_plane->plane;
  255. u32 plane_ctl;
  256. key->min_value = I915_READ(PLANE_KEYVAL(pipe, plane));
  257. key->max_value = I915_READ(PLANE_KEYMAX(pipe, plane));
  258. key->channel_mask = I915_READ(PLANE_KEYMSK(pipe, plane));
  259. plane_ctl = I915_READ(PLANE_CTL(pipe, plane));
  260. switch (plane_ctl & PLANE_CTL_KEY_ENABLE_MASK) {
  261. case PLANE_CTL_KEY_ENABLE_DESTINATION:
  262. key->flags = I915_SET_COLORKEY_DESTINATION;
  263. break;
  264. case PLANE_CTL_KEY_ENABLE_SOURCE:
  265. key->flags = I915_SET_COLORKEY_SOURCE;
  266. break;
  267. default:
  268. key->flags = I915_SET_COLORKEY_NONE;
  269. }
  270. }
  271. static void
  272. vlv_update_plane(struct drm_plane *dplane, struct drm_crtc *crtc,
  273. struct drm_framebuffer *fb,
  274. struct drm_i915_gem_object *obj, int crtc_x, int crtc_y,
  275. unsigned int crtc_w, unsigned int crtc_h,
  276. uint32_t x, uint32_t y,
  277. uint32_t src_w, uint32_t src_h)
  278. {
  279. struct drm_device *dev = dplane->dev;
  280. struct drm_i915_private *dev_priv = dev->dev_private;
  281. struct intel_plane *intel_plane = to_intel_plane(dplane);
  282. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  283. int pipe = intel_plane->pipe;
  284. int plane = intel_plane->plane;
  285. u32 sprctl;
  286. unsigned long sprsurf_offset, linear_offset;
  287. int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  288. u32 start_vbl_count;
  289. bool atomic_update;
  290. sprctl = I915_READ(SPCNTR(pipe, plane));
  291. /* Mask out pixel format bits in case we change it */
  292. sprctl &= ~SP_PIXFORMAT_MASK;
  293. sprctl &= ~SP_YUV_BYTE_ORDER_MASK;
  294. sprctl &= ~SP_TILED;
  295. sprctl &= ~SP_ROTATE_180;
  296. switch (fb->pixel_format) {
  297. case DRM_FORMAT_YUYV:
  298. sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_YUYV;
  299. break;
  300. case DRM_FORMAT_YVYU:
  301. sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_YVYU;
  302. break;
  303. case DRM_FORMAT_UYVY:
  304. sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_UYVY;
  305. break;
  306. case DRM_FORMAT_VYUY:
  307. sprctl |= SP_FORMAT_YUV422 | SP_YUV_ORDER_VYUY;
  308. break;
  309. case DRM_FORMAT_RGB565:
  310. sprctl |= SP_FORMAT_BGR565;
  311. break;
  312. case DRM_FORMAT_XRGB8888:
  313. sprctl |= SP_FORMAT_BGRX8888;
  314. break;
  315. case DRM_FORMAT_ARGB8888:
  316. sprctl |= SP_FORMAT_BGRA8888;
  317. break;
  318. case DRM_FORMAT_XBGR2101010:
  319. sprctl |= SP_FORMAT_RGBX1010102;
  320. break;
  321. case DRM_FORMAT_ABGR2101010:
  322. sprctl |= SP_FORMAT_RGBA1010102;
  323. break;
  324. case DRM_FORMAT_XBGR8888:
  325. sprctl |= SP_FORMAT_RGBX8888;
  326. break;
  327. case DRM_FORMAT_ABGR8888:
  328. sprctl |= SP_FORMAT_RGBA8888;
  329. break;
  330. default:
  331. /*
  332. * If we get here one of the upper layers failed to filter
  333. * out the unsupported plane formats
  334. */
  335. BUG();
  336. break;
  337. }
  338. /*
  339. * Enable gamma to match primary/cursor plane behaviour.
  340. * FIXME should be user controllable via propertiesa.
  341. */
  342. sprctl |= SP_GAMMA_ENABLE;
  343. if (obj->tiling_mode != I915_TILING_NONE)
  344. sprctl |= SP_TILED;
  345. sprctl |= SP_ENABLE;
  346. intel_update_sprite_watermarks(dplane, crtc, src_w, src_h,
  347. pixel_size, true,
  348. src_w != crtc_w || src_h != crtc_h);
  349. /* Sizes are 0 based */
  350. src_w--;
  351. src_h--;
  352. crtc_w--;
  353. crtc_h--;
  354. linear_offset = y * fb->pitches[0] + x * pixel_size;
  355. sprsurf_offset = intel_gen4_compute_page_offset(&x, &y,
  356. obj->tiling_mode,
  357. pixel_size,
  358. fb->pitches[0]);
  359. linear_offset -= sprsurf_offset;
  360. if (intel_plane->rotation == BIT(DRM_ROTATE_180)) {
  361. sprctl |= SP_ROTATE_180;
  362. x += src_w;
  363. y += src_h;
  364. linear_offset += src_h * fb->pitches[0] + src_w * pixel_size;
  365. }
  366. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  367. intel_update_primary_plane(intel_crtc);
  368. I915_WRITE(SPSTRIDE(pipe, plane), fb->pitches[0]);
  369. I915_WRITE(SPPOS(pipe, plane), (crtc_y << 16) | crtc_x);
  370. if (obj->tiling_mode != I915_TILING_NONE)
  371. I915_WRITE(SPTILEOFF(pipe, plane), (y << 16) | x);
  372. else
  373. I915_WRITE(SPLINOFF(pipe, plane), linear_offset);
  374. I915_WRITE(SPSIZE(pipe, plane), (crtc_h << 16) | crtc_w);
  375. I915_WRITE(SPCNTR(pipe, plane), sprctl);
  376. I915_WRITE(SPSURF(pipe, plane), i915_gem_obj_ggtt_offset(obj) +
  377. sprsurf_offset);
  378. intel_flush_primary_plane(dev_priv, intel_crtc->plane);
  379. if (atomic_update)
  380. intel_pipe_update_end(intel_crtc, start_vbl_count);
  381. }
  382. static void
  383. vlv_disable_plane(struct drm_plane *dplane, struct drm_crtc *crtc)
  384. {
  385. struct drm_device *dev = dplane->dev;
  386. struct drm_i915_private *dev_priv = dev->dev_private;
  387. struct intel_plane *intel_plane = to_intel_plane(dplane);
  388. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  389. int pipe = intel_plane->pipe;
  390. int plane = intel_plane->plane;
  391. u32 start_vbl_count;
  392. bool atomic_update;
  393. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  394. intel_update_primary_plane(intel_crtc);
  395. I915_WRITE(SPCNTR(pipe, plane), I915_READ(SPCNTR(pipe, plane)) &
  396. ~SP_ENABLE);
  397. /* Activate double buffered register update */
  398. I915_WRITE(SPSURF(pipe, plane), 0);
  399. intel_flush_primary_plane(dev_priv, intel_crtc->plane);
  400. if (atomic_update)
  401. intel_pipe_update_end(intel_crtc, start_vbl_count);
  402. intel_update_sprite_watermarks(dplane, crtc, 0, 0, 0, false, false);
  403. }
  404. static int
  405. vlv_update_colorkey(struct drm_plane *dplane,
  406. struct drm_intel_sprite_colorkey *key)
  407. {
  408. struct drm_device *dev = dplane->dev;
  409. struct drm_i915_private *dev_priv = dev->dev_private;
  410. struct intel_plane *intel_plane = to_intel_plane(dplane);
  411. int pipe = intel_plane->pipe;
  412. int plane = intel_plane->plane;
  413. u32 sprctl;
  414. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  415. return -EINVAL;
  416. I915_WRITE(SPKEYMINVAL(pipe, plane), key->min_value);
  417. I915_WRITE(SPKEYMAXVAL(pipe, plane), key->max_value);
  418. I915_WRITE(SPKEYMSK(pipe, plane), key->channel_mask);
  419. sprctl = I915_READ(SPCNTR(pipe, plane));
  420. sprctl &= ~SP_SOURCE_KEY;
  421. if (key->flags & I915_SET_COLORKEY_SOURCE)
  422. sprctl |= SP_SOURCE_KEY;
  423. I915_WRITE(SPCNTR(pipe, plane), sprctl);
  424. POSTING_READ(SPKEYMSK(pipe, plane));
  425. return 0;
  426. }
  427. static void
  428. vlv_get_colorkey(struct drm_plane *dplane,
  429. struct drm_intel_sprite_colorkey *key)
  430. {
  431. struct drm_device *dev = dplane->dev;
  432. struct drm_i915_private *dev_priv = dev->dev_private;
  433. struct intel_plane *intel_plane = to_intel_plane(dplane);
  434. int pipe = intel_plane->pipe;
  435. int plane = intel_plane->plane;
  436. u32 sprctl;
  437. key->min_value = I915_READ(SPKEYMINVAL(pipe, plane));
  438. key->max_value = I915_READ(SPKEYMAXVAL(pipe, plane));
  439. key->channel_mask = I915_READ(SPKEYMSK(pipe, plane));
  440. sprctl = I915_READ(SPCNTR(pipe, plane));
  441. if (sprctl & SP_SOURCE_KEY)
  442. key->flags = I915_SET_COLORKEY_SOURCE;
  443. else
  444. key->flags = I915_SET_COLORKEY_NONE;
  445. }
  446. static void
  447. ivb_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
  448. struct drm_framebuffer *fb,
  449. struct drm_i915_gem_object *obj, int crtc_x, int crtc_y,
  450. unsigned int crtc_w, unsigned int crtc_h,
  451. uint32_t x, uint32_t y,
  452. uint32_t src_w, uint32_t src_h)
  453. {
  454. struct drm_device *dev = plane->dev;
  455. struct drm_i915_private *dev_priv = dev->dev_private;
  456. struct intel_plane *intel_plane = to_intel_plane(plane);
  457. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  458. int pipe = intel_plane->pipe;
  459. u32 sprctl, sprscale = 0;
  460. unsigned long sprsurf_offset, linear_offset;
  461. int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  462. u32 start_vbl_count;
  463. bool atomic_update;
  464. sprctl = I915_READ(SPRCTL(pipe));
  465. /* Mask out pixel format bits in case we change it */
  466. sprctl &= ~SPRITE_PIXFORMAT_MASK;
  467. sprctl &= ~SPRITE_RGB_ORDER_RGBX;
  468. sprctl &= ~SPRITE_YUV_BYTE_ORDER_MASK;
  469. sprctl &= ~SPRITE_TILED;
  470. sprctl &= ~SPRITE_ROTATE_180;
  471. switch (fb->pixel_format) {
  472. case DRM_FORMAT_XBGR8888:
  473. sprctl |= SPRITE_FORMAT_RGBX888 | SPRITE_RGB_ORDER_RGBX;
  474. break;
  475. case DRM_FORMAT_XRGB8888:
  476. sprctl |= SPRITE_FORMAT_RGBX888;
  477. break;
  478. case DRM_FORMAT_YUYV:
  479. sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YUYV;
  480. break;
  481. case DRM_FORMAT_YVYU:
  482. sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_YVYU;
  483. break;
  484. case DRM_FORMAT_UYVY:
  485. sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_UYVY;
  486. break;
  487. case DRM_FORMAT_VYUY:
  488. sprctl |= SPRITE_FORMAT_YUV422 | SPRITE_YUV_ORDER_VYUY;
  489. break;
  490. default:
  491. BUG();
  492. }
  493. /*
  494. * Enable gamma to match primary/cursor plane behaviour.
  495. * FIXME should be user controllable via propertiesa.
  496. */
  497. sprctl |= SPRITE_GAMMA_ENABLE;
  498. if (obj->tiling_mode != I915_TILING_NONE)
  499. sprctl |= SPRITE_TILED;
  500. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  501. sprctl &= ~SPRITE_TRICKLE_FEED_DISABLE;
  502. else
  503. sprctl |= SPRITE_TRICKLE_FEED_DISABLE;
  504. sprctl |= SPRITE_ENABLE;
  505. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  506. sprctl |= SPRITE_PIPE_CSC_ENABLE;
  507. intel_update_sprite_watermarks(plane, crtc, src_w, src_h, pixel_size,
  508. true,
  509. src_w != crtc_w || src_h != crtc_h);
  510. /* Sizes are 0 based */
  511. src_w--;
  512. src_h--;
  513. crtc_w--;
  514. crtc_h--;
  515. if (crtc_w != src_w || crtc_h != src_h)
  516. sprscale = SPRITE_SCALE_ENABLE | (src_w << 16) | src_h;
  517. linear_offset = y * fb->pitches[0] + x * pixel_size;
  518. sprsurf_offset =
  519. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  520. pixel_size, fb->pitches[0]);
  521. linear_offset -= sprsurf_offset;
  522. if (intel_plane->rotation == BIT(DRM_ROTATE_180)) {
  523. sprctl |= SPRITE_ROTATE_180;
  524. /* HSW and BDW does this automagically in hardware */
  525. if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
  526. x += src_w;
  527. y += src_h;
  528. linear_offset += src_h * fb->pitches[0] +
  529. src_w * pixel_size;
  530. }
  531. }
  532. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  533. intel_update_primary_plane(intel_crtc);
  534. I915_WRITE(SPRSTRIDE(pipe), fb->pitches[0]);
  535. I915_WRITE(SPRPOS(pipe), (crtc_y << 16) | crtc_x);
  536. /* HSW consolidates SPRTILEOFF and SPRLINOFF into a single SPROFFSET
  537. * register */
  538. if (IS_HASWELL(dev) || IS_BROADWELL(dev))
  539. I915_WRITE(SPROFFSET(pipe), (y << 16) | x);
  540. else if (obj->tiling_mode != I915_TILING_NONE)
  541. I915_WRITE(SPRTILEOFF(pipe), (y << 16) | x);
  542. else
  543. I915_WRITE(SPRLINOFF(pipe), linear_offset);
  544. I915_WRITE(SPRSIZE(pipe), (crtc_h << 16) | crtc_w);
  545. if (intel_plane->can_scale)
  546. I915_WRITE(SPRSCALE(pipe), sprscale);
  547. I915_WRITE(SPRCTL(pipe), sprctl);
  548. I915_WRITE(SPRSURF(pipe),
  549. i915_gem_obj_ggtt_offset(obj) + sprsurf_offset);
  550. intel_flush_primary_plane(dev_priv, intel_crtc->plane);
  551. if (atomic_update)
  552. intel_pipe_update_end(intel_crtc, start_vbl_count);
  553. }
  554. static void
  555. ivb_disable_plane(struct drm_plane *plane, struct drm_crtc *crtc)
  556. {
  557. struct drm_device *dev = plane->dev;
  558. struct drm_i915_private *dev_priv = dev->dev_private;
  559. struct intel_plane *intel_plane = to_intel_plane(plane);
  560. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  561. int pipe = intel_plane->pipe;
  562. u32 start_vbl_count;
  563. bool atomic_update;
  564. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  565. intel_update_primary_plane(intel_crtc);
  566. I915_WRITE(SPRCTL(pipe), I915_READ(SPRCTL(pipe)) & ~SPRITE_ENABLE);
  567. /* Can't leave the scaler enabled... */
  568. if (intel_plane->can_scale)
  569. I915_WRITE(SPRSCALE(pipe), 0);
  570. /* Activate double buffered register update */
  571. I915_WRITE(SPRSURF(pipe), 0);
  572. intel_flush_primary_plane(dev_priv, intel_crtc->plane);
  573. if (atomic_update)
  574. intel_pipe_update_end(intel_crtc, start_vbl_count);
  575. /*
  576. * Avoid underruns when disabling the sprite.
  577. * FIXME remove once watermark updates are done properly.
  578. */
  579. intel_wait_for_vblank(dev, pipe);
  580. intel_update_sprite_watermarks(plane, crtc, 0, 0, 0, false, false);
  581. }
  582. static int
  583. ivb_update_colorkey(struct drm_plane *plane,
  584. struct drm_intel_sprite_colorkey *key)
  585. {
  586. struct drm_device *dev = plane->dev;
  587. struct drm_i915_private *dev_priv = dev->dev_private;
  588. struct intel_plane *intel_plane;
  589. u32 sprctl;
  590. int ret = 0;
  591. intel_plane = to_intel_plane(plane);
  592. I915_WRITE(SPRKEYVAL(intel_plane->pipe), key->min_value);
  593. I915_WRITE(SPRKEYMAX(intel_plane->pipe), key->max_value);
  594. I915_WRITE(SPRKEYMSK(intel_plane->pipe), key->channel_mask);
  595. sprctl = I915_READ(SPRCTL(intel_plane->pipe));
  596. sprctl &= ~(SPRITE_SOURCE_KEY | SPRITE_DEST_KEY);
  597. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  598. sprctl |= SPRITE_DEST_KEY;
  599. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  600. sprctl |= SPRITE_SOURCE_KEY;
  601. I915_WRITE(SPRCTL(intel_plane->pipe), sprctl);
  602. POSTING_READ(SPRKEYMSK(intel_plane->pipe));
  603. return ret;
  604. }
  605. static void
  606. ivb_get_colorkey(struct drm_plane *plane, struct drm_intel_sprite_colorkey *key)
  607. {
  608. struct drm_device *dev = plane->dev;
  609. struct drm_i915_private *dev_priv = dev->dev_private;
  610. struct intel_plane *intel_plane;
  611. u32 sprctl;
  612. intel_plane = to_intel_plane(plane);
  613. key->min_value = I915_READ(SPRKEYVAL(intel_plane->pipe));
  614. key->max_value = I915_READ(SPRKEYMAX(intel_plane->pipe));
  615. key->channel_mask = I915_READ(SPRKEYMSK(intel_plane->pipe));
  616. key->flags = 0;
  617. sprctl = I915_READ(SPRCTL(intel_plane->pipe));
  618. if (sprctl & SPRITE_DEST_KEY)
  619. key->flags = I915_SET_COLORKEY_DESTINATION;
  620. else if (sprctl & SPRITE_SOURCE_KEY)
  621. key->flags = I915_SET_COLORKEY_SOURCE;
  622. else
  623. key->flags = I915_SET_COLORKEY_NONE;
  624. }
  625. static void
  626. ilk_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
  627. struct drm_framebuffer *fb,
  628. struct drm_i915_gem_object *obj, int crtc_x, int crtc_y,
  629. unsigned int crtc_w, unsigned int crtc_h,
  630. uint32_t x, uint32_t y,
  631. uint32_t src_w, uint32_t src_h)
  632. {
  633. struct drm_device *dev = plane->dev;
  634. struct drm_i915_private *dev_priv = dev->dev_private;
  635. struct intel_plane *intel_plane = to_intel_plane(plane);
  636. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  637. int pipe = intel_plane->pipe;
  638. unsigned long dvssurf_offset, linear_offset;
  639. u32 dvscntr, dvsscale;
  640. int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  641. u32 start_vbl_count;
  642. bool atomic_update;
  643. dvscntr = I915_READ(DVSCNTR(pipe));
  644. /* Mask out pixel format bits in case we change it */
  645. dvscntr &= ~DVS_PIXFORMAT_MASK;
  646. dvscntr &= ~DVS_RGB_ORDER_XBGR;
  647. dvscntr &= ~DVS_YUV_BYTE_ORDER_MASK;
  648. dvscntr &= ~DVS_TILED;
  649. dvscntr &= ~DVS_ROTATE_180;
  650. switch (fb->pixel_format) {
  651. case DRM_FORMAT_XBGR8888:
  652. dvscntr |= DVS_FORMAT_RGBX888 | DVS_RGB_ORDER_XBGR;
  653. break;
  654. case DRM_FORMAT_XRGB8888:
  655. dvscntr |= DVS_FORMAT_RGBX888;
  656. break;
  657. case DRM_FORMAT_YUYV:
  658. dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YUYV;
  659. break;
  660. case DRM_FORMAT_YVYU:
  661. dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_YVYU;
  662. break;
  663. case DRM_FORMAT_UYVY:
  664. dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_UYVY;
  665. break;
  666. case DRM_FORMAT_VYUY:
  667. dvscntr |= DVS_FORMAT_YUV422 | DVS_YUV_ORDER_VYUY;
  668. break;
  669. default:
  670. BUG();
  671. }
  672. /*
  673. * Enable gamma to match primary/cursor plane behaviour.
  674. * FIXME should be user controllable via propertiesa.
  675. */
  676. dvscntr |= DVS_GAMMA_ENABLE;
  677. if (obj->tiling_mode != I915_TILING_NONE)
  678. dvscntr |= DVS_TILED;
  679. if (IS_GEN6(dev))
  680. dvscntr |= DVS_TRICKLE_FEED_DISABLE; /* must disable */
  681. dvscntr |= DVS_ENABLE;
  682. intel_update_sprite_watermarks(plane, crtc, src_w, src_h,
  683. pixel_size, true,
  684. src_w != crtc_w || src_h != crtc_h);
  685. /* Sizes are 0 based */
  686. src_w--;
  687. src_h--;
  688. crtc_w--;
  689. crtc_h--;
  690. dvsscale = 0;
  691. if (crtc_w != src_w || crtc_h != src_h)
  692. dvsscale = DVS_SCALE_ENABLE | (src_w << 16) | src_h;
  693. linear_offset = y * fb->pitches[0] + x * pixel_size;
  694. dvssurf_offset =
  695. intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
  696. pixel_size, fb->pitches[0]);
  697. linear_offset -= dvssurf_offset;
  698. if (intel_plane->rotation == BIT(DRM_ROTATE_180)) {
  699. dvscntr |= DVS_ROTATE_180;
  700. x += src_w;
  701. y += src_h;
  702. linear_offset += src_h * fb->pitches[0] + src_w * pixel_size;
  703. }
  704. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  705. intel_update_primary_plane(intel_crtc);
  706. I915_WRITE(DVSSTRIDE(pipe), fb->pitches[0]);
  707. I915_WRITE(DVSPOS(pipe), (crtc_y << 16) | crtc_x);
  708. if (obj->tiling_mode != I915_TILING_NONE)
  709. I915_WRITE(DVSTILEOFF(pipe), (y << 16) | x);
  710. else
  711. I915_WRITE(DVSLINOFF(pipe), linear_offset);
  712. I915_WRITE(DVSSIZE(pipe), (crtc_h << 16) | crtc_w);
  713. I915_WRITE(DVSSCALE(pipe), dvsscale);
  714. I915_WRITE(DVSCNTR(pipe), dvscntr);
  715. I915_WRITE(DVSSURF(pipe),
  716. i915_gem_obj_ggtt_offset(obj) + dvssurf_offset);
  717. intel_flush_primary_plane(dev_priv, intel_crtc->plane);
  718. if (atomic_update)
  719. intel_pipe_update_end(intel_crtc, start_vbl_count);
  720. }
  721. static void
  722. ilk_disable_plane(struct drm_plane *plane, struct drm_crtc *crtc)
  723. {
  724. struct drm_device *dev = plane->dev;
  725. struct drm_i915_private *dev_priv = dev->dev_private;
  726. struct intel_plane *intel_plane = to_intel_plane(plane);
  727. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  728. int pipe = intel_plane->pipe;
  729. u32 start_vbl_count;
  730. bool atomic_update;
  731. atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
  732. intel_update_primary_plane(intel_crtc);
  733. I915_WRITE(DVSCNTR(pipe), I915_READ(DVSCNTR(pipe)) & ~DVS_ENABLE);
  734. /* Disable the scaler */
  735. I915_WRITE(DVSSCALE(pipe), 0);
  736. /* Flush double buffered register updates */
  737. I915_WRITE(DVSSURF(pipe), 0);
  738. intel_flush_primary_plane(dev_priv, intel_crtc->plane);
  739. if (atomic_update)
  740. intel_pipe_update_end(intel_crtc, start_vbl_count);
  741. /*
  742. * Avoid underruns when disabling the sprite.
  743. * FIXME remove once watermark updates are done properly.
  744. */
  745. intel_wait_for_vblank(dev, pipe);
  746. intel_update_sprite_watermarks(plane, crtc, 0, 0, 0, false, false);
  747. }
  748. static void
  749. intel_post_enable_primary(struct drm_crtc *crtc)
  750. {
  751. struct drm_device *dev = crtc->dev;
  752. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  753. /*
  754. * BDW signals flip done immediately if the plane
  755. * is disabled, even if the plane enable is already
  756. * armed to occur at the next vblank :(
  757. */
  758. if (IS_BROADWELL(dev))
  759. intel_wait_for_vblank(dev, intel_crtc->pipe);
  760. /*
  761. * FIXME IPS should be fine as long as one plane is
  762. * enabled, but in practice it seems to have problems
  763. * when going from primary only to sprite only and vice
  764. * versa.
  765. */
  766. hsw_enable_ips(intel_crtc);
  767. mutex_lock(&dev->struct_mutex);
  768. intel_update_fbc(dev);
  769. mutex_unlock(&dev->struct_mutex);
  770. }
  771. static void
  772. intel_pre_disable_primary(struct drm_crtc *crtc)
  773. {
  774. struct drm_device *dev = crtc->dev;
  775. struct drm_i915_private *dev_priv = dev->dev_private;
  776. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  777. mutex_lock(&dev->struct_mutex);
  778. if (dev_priv->fbc.plane == intel_crtc->plane)
  779. intel_disable_fbc(dev);
  780. mutex_unlock(&dev->struct_mutex);
  781. /*
  782. * FIXME IPS should be fine as long as one plane is
  783. * enabled, but in practice it seems to have problems
  784. * when going from primary only to sprite only and vice
  785. * versa.
  786. */
  787. hsw_disable_ips(intel_crtc);
  788. }
  789. static int
  790. ilk_update_colorkey(struct drm_plane *plane,
  791. struct drm_intel_sprite_colorkey *key)
  792. {
  793. struct drm_device *dev = plane->dev;
  794. struct drm_i915_private *dev_priv = dev->dev_private;
  795. struct intel_plane *intel_plane;
  796. u32 dvscntr;
  797. int ret = 0;
  798. intel_plane = to_intel_plane(plane);
  799. I915_WRITE(DVSKEYVAL(intel_plane->pipe), key->min_value);
  800. I915_WRITE(DVSKEYMAX(intel_plane->pipe), key->max_value);
  801. I915_WRITE(DVSKEYMSK(intel_plane->pipe), key->channel_mask);
  802. dvscntr = I915_READ(DVSCNTR(intel_plane->pipe));
  803. dvscntr &= ~(DVS_SOURCE_KEY | DVS_DEST_KEY);
  804. if (key->flags & I915_SET_COLORKEY_DESTINATION)
  805. dvscntr |= DVS_DEST_KEY;
  806. else if (key->flags & I915_SET_COLORKEY_SOURCE)
  807. dvscntr |= DVS_SOURCE_KEY;
  808. I915_WRITE(DVSCNTR(intel_plane->pipe), dvscntr);
  809. POSTING_READ(DVSKEYMSK(intel_plane->pipe));
  810. return ret;
  811. }
  812. static void
  813. ilk_get_colorkey(struct drm_plane *plane, struct drm_intel_sprite_colorkey *key)
  814. {
  815. struct drm_device *dev = plane->dev;
  816. struct drm_i915_private *dev_priv = dev->dev_private;
  817. struct intel_plane *intel_plane;
  818. u32 dvscntr;
  819. intel_plane = to_intel_plane(plane);
  820. key->min_value = I915_READ(DVSKEYVAL(intel_plane->pipe));
  821. key->max_value = I915_READ(DVSKEYMAX(intel_plane->pipe));
  822. key->channel_mask = I915_READ(DVSKEYMSK(intel_plane->pipe));
  823. key->flags = 0;
  824. dvscntr = I915_READ(DVSCNTR(intel_plane->pipe));
  825. if (dvscntr & DVS_DEST_KEY)
  826. key->flags = I915_SET_COLORKEY_DESTINATION;
  827. else if (dvscntr & DVS_SOURCE_KEY)
  828. key->flags = I915_SET_COLORKEY_SOURCE;
  829. else
  830. key->flags = I915_SET_COLORKEY_NONE;
  831. }
  832. static bool
  833. format_is_yuv(uint32_t format)
  834. {
  835. switch (format) {
  836. case DRM_FORMAT_YUYV:
  837. case DRM_FORMAT_UYVY:
  838. case DRM_FORMAT_VYUY:
  839. case DRM_FORMAT_YVYU:
  840. return true;
  841. default:
  842. return false;
  843. }
  844. }
  845. static bool colorkey_enabled(struct intel_plane *intel_plane)
  846. {
  847. struct drm_intel_sprite_colorkey key;
  848. intel_plane->get_colorkey(&intel_plane->base, &key);
  849. return key.flags != I915_SET_COLORKEY_NONE;
  850. }
  851. static int
  852. intel_check_sprite_plane(struct drm_plane *plane,
  853. struct intel_plane_state *state)
  854. {
  855. struct intel_crtc *intel_crtc = to_intel_crtc(state->crtc);
  856. struct intel_plane *intel_plane = to_intel_plane(plane);
  857. struct drm_framebuffer *fb = state->fb;
  858. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  859. struct drm_i915_gem_object *obj = intel_fb->obj;
  860. int crtc_x, crtc_y;
  861. unsigned int crtc_w, crtc_h;
  862. uint32_t src_x, src_y, src_w, src_h;
  863. struct drm_rect *src = &state->src;
  864. struct drm_rect *dst = &state->dst;
  865. struct drm_rect *orig_src = &state->orig_src;
  866. const struct drm_rect *clip = &state->clip;
  867. int hscale, vscale;
  868. int max_scale, min_scale;
  869. int pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
  870. /* Don't modify another pipe's plane */
  871. if (intel_plane->pipe != intel_crtc->pipe) {
  872. DRM_DEBUG_KMS("Wrong plane <-> crtc mapping\n");
  873. return -EINVAL;
  874. }
  875. /* FIXME check all gen limits */
  876. if (fb->width < 3 || fb->height < 3 || fb->pitches[0] > 16384) {
  877. DRM_DEBUG_KMS("Unsuitable framebuffer for plane\n");
  878. return -EINVAL;
  879. }
  880. /* Sprite planes can be linear or x-tiled surfaces */
  881. switch (obj->tiling_mode) {
  882. case I915_TILING_NONE:
  883. case I915_TILING_X:
  884. break;
  885. default:
  886. DRM_DEBUG_KMS("Unsupported tiling mode\n");
  887. return -EINVAL;
  888. }
  889. /*
  890. * FIXME the following code does a bunch of fuzzy adjustments to the
  891. * coordinates and sizes. We probably need some way to decide whether
  892. * more strict checking should be done instead.
  893. */
  894. max_scale = intel_plane->max_downscale << 16;
  895. min_scale = intel_plane->can_scale ? 1 : (1 << 16);
  896. drm_rect_rotate(src, fb->width << 16, fb->height << 16,
  897. intel_plane->rotation);
  898. hscale = drm_rect_calc_hscale_relaxed(src, dst, min_scale, max_scale);
  899. BUG_ON(hscale < 0);
  900. vscale = drm_rect_calc_vscale_relaxed(src, dst, min_scale, max_scale);
  901. BUG_ON(vscale < 0);
  902. state->visible = drm_rect_clip_scaled(src, dst, clip, hscale, vscale);
  903. crtc_x = dst->x1;
  904. crtc_y = dst->y1;
  905. crtc_w = drm_rect_width(dst);
  906. crtc_h = drm_rect_height(dst);
  907. if (state->visible) {
  908. /* check again in case clipping clamped the results */
  909. hscale = drm_rect_calc_hscale(src, dst, min_scale, max_scale);
  910. if (hscale < 0) {
  911. DRM_DEBUG_KMS("Horizontal scaling factor out of limits\n");
  912. drm_rect_debug_print(src, true);
  913. drm_rect_debug_print(dst, false);
  914. return hscale;
  915. }
  916. vscale = drm_rect_calc_vscale(src, dst, min_scale, max_scale);
  917. if (vscale < 0) {
  918. DRM_DEBUG_KMS("Vertical scaling factor out of limits\n");
  919. drm_rect_debug_print(src, true);
  920. drm_rect_debug_print(dst, false);
  921. return vscale;
  922. }
  923. /* Make the source viewport size an exact multiple of the scaling factors. */
  924. drm_rect_adjust_size(src,
  925. drm_rect_width(dst) * hscale - drm_rect_width(src),
  926. drm_rect_height(dst) * vscale - drm_rect_height(src));
  927. drm_rect_rotate_inv(src, fb->width << 16, fb->height << 16,
  928. intel_plane->rotation);
  929. /* sanity check to make sure the src viewport wasn't enlarged */
  930. WARN_ON(src->x1 < (int) orig_src->x1 ||
  931. src->y1 < (int) orig_src->y1 ||
  932. src->x2 > (int) orig_src->x2 ||
  933. src->y2 > (int) orig_src->y2);
  934. /*
  935. * Hardware doesn't handle subpixel coordinates.
  936. * Adjust to (macro)pixel boundary, but be careful not to
  937. * increase the source viewport size, because that could
  938. * push the downscaling factor out of bounds.
  939. */
  940. src_x = src->x1 >> 16;
  941. src_w = drm_rect_width(src) >> 16;
  942. src_y = src->y1 >> 16;
  943. src_h = drm_rect_height(src) >> 16;
  944. if (format_is_yuv(fb->pixel_format)) {
  945. src_x &= ~1;
  946. src_w &= ~1;
  947. /*
  948. * Must keep src and dst the
  949. * same if we can't scale.
  950. */
  951. if (!intel_plane->can_scale)
  952. crtc_w &= ~1;
  953. if (crtc_w == 0)
  954. state->visible = false;
  955. }
  956. }
  957. /* Check size restrictions when scaling */
  958. if (state->visible && (src_w != crtc_w || src_h != crtc_h)) {
  959. unsigned int width_bytes;
  960. WARN_ON(!intel_plane->can_scale);
  961. /* FIXME interlacing min height is 6 */
  962. if (crtc_w < 3 || crtc_h < 3)
  963. state->visible = false;
  964. if (src_w < 3 || src_h < 3)
  965. state->visible = false;
  966. width_bytes = ((src_x * pixel_size) & 63) +
  967. src_w * pixel_size;
  968. if (src_w > 2048 || src_h > 2048 ||
  969. width_bytes > 4096 || fb->pitches[0] > 4096) {
  970. DRM_DEBUG_KMS("Source dimensions exceed hardware limits\n");
  971. return -EINVAL;
  972. }
  973. }
  974. if (state->visible) {
  975. src->x1 = src_x;
  976. src->x2 = src_x + src_w;
  977. src->y1 = src_y;
  978. src->y2 = src_y + src_h;
  979. }
  980. dst->x1 = crtc_x;
  981. dst->x2 = crtc_x + crtc_w;
  982. dst->y1 = crtc_y;
  983. dst->y2 = crtc_y + crtc_h;
  984. return 0;
  985. }
  986. static int
  987. intel_commit_sprite_plane(struct drm_plane *plane,
  988. struct intel_plane_state *state)
  989. {
  990. struct drm_device *dev = plane->dev;
  991. struct drm_crtc *crtc = state->crtc;
  992. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  993. struct intel_plane *intel_plane = to_intel_plane(plane);
  994. enum pipe pipe = intel_crtc->pipe;
  995. struct drm_framebuffer *fb = state->fb;
  996. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  997. struct drm_i915_gem_object *obj = intel_fb->obj;
  998. struct drm_i915_gem_object *old_obj = intel_plane->obj;
  999. int crtc_x, crtc_y;
  1000. unsigned int crtc_w, crtc_h;
  1001. uint32_t src_x, src_y, src_w, src_h;
  1002. struct drm_rect *dst = &state->dst;
  1003. const struct drm_rect *clip = &state->clip;
  1004. bool primary_enabled;
  1005. int ret;
  1006. /*
  1007. * If the sprite is completely covering the primary plane,
  1008. * we can disable the primary and save power.
  1009. */
  1010. primary_enabled = !drm_rect_equals(dst, clip) || colorkey_enabled(intel_plane);
  1011. WARN_ON(!primary_enabled && !state->visible && intel_crtc->active);
  1012. if (old_obj != obj) {
  1013. mutex_lock(&dev->struct_mutex);
  1014. /* Note that this will apply the VT-d workaround for scanouts,
  1015. * which is more restrictive than required for sprites. (The
  1016. * primary plane requires 256KiB alignment with 64 PTE padding,
  1017. * the sprite planes only require 128KiB alignment and 32 PTE
  1018. * padding.
  1019. */
  1020. ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
  1021. if (ret == 0)
  1022. i915_gem_track_fb(old_obj, obj,
  1023. INTEL_FRONTBUFFER_SPRITE(pipe));
  1024. mutex_unlock(&dev->struct_mutex);
  1025. if (ret)
  1026. return ret;
  1027. }
  1028. intel_plane->crtc_x = state->orig_dst.x1;
  1029. intel_plane->crtc_y = state->orig_dst.y1;
  1030. intel_plane->crtc_w = drm_rect_width(&state->orig_dst);
  1031. intel_plane->crtc_h = drm_rect_height(&state->orig_dst);
  1032. intel_plane->src_x = state->orig_src.x1;
  1033. intel_plane->src_y = state->orig_src.y1;
  1034. intel_plane->src_w = drm_rect_width(&state->orig_src);
  1035. intel_plane->src_h = drm_rect_height(&state->orig_src);
  1036. intel_plane->obj = obj;
  1037. if (intel_crtc->active) {
  1038. bool primary_was_enabled = intel_crtc->primary_enabled;
  1039. intel_crtc->primary_enabled = primary_enabled;
  1040. if (primary_was_enabled != primary_enabled)
  1041. intel_crtc_wait_for_pending_flips(crtc);
  1042. if (primary_was_enabled && !primary_enabled)
  1043. intel_pre_disable_primary(crtc);
  1044. if (state->visible) {
  1045. crtc_x = state->dst.x1;
  1046. crtc_y = state->dst.y1;
  1047. crtc_w = drm_rect_width(&state->dst);
  1048. crtc_h = drm_rect_height(&state->dst);
  1049. src_x = state->src.x1;
  1050. src_y = state->src.y1;
  1051. src_w = drm_rect_width(&state->src);
  1052. src_h = drm_rect_height(&state->src);
  1053. intel_plane->update_plane(plane, crtc, fb, obj,
  1054. crtc_x, crtc_y, crtc_w, crtc_h,
  1055. src_x, src_y, src_w, src_h);
  1056. } else {
  1057. intel_plane->disable_plane(plane, crtc);
  1058. }
  1059. intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_SPRITE(pipe));
  1060. if (!primary_was_enabled && primary_enabled)
  1061. intel_post_enable_primary(crtc);
  1062. }
  1063. /* Unpin old obj after new one is active to avoid ugliness */
  1064. if (old_obj && old_obj != obj) {
  1065. /*
  1066. * It's fairly common to simply update the position of
  1067. * an existing object. In that case, we don't need to
  1068. * wait for vblank to avoid ugliness, we only need to
  1069. * do the pin & ref bookkeeping.
  1070. */
  1071. if (intel_crtc->active)
  1072. intel_wait_for_vblank(dev, intel_crtc->pipe);
  1073. mutex_lock(&dev->struct_mutex);
  1074. intel_unpin_fb_obj(old_obj);
  1075. mutex_unlock(&dev->struct_mutex);
  1076. }
  1077. return 0;
  1078. }
  1079. static int
  1080. intel_update_plane(struct drm_plane *plane, struct drm_crtc *crtc,
  1081. struct drm_framebuffer *fb, int crtc_x, int crtc_y,
  1082. unsigned int crtc_w, unsigned int crtc_h,
  1083. uint32_t src_x, uint32_t src_y,
  1084. uint32_t src_w, uint32_t src_h)
  1085. {
  1086. struct intel_plane_state state;
  1087. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1088. int ret;
  1089. state.crtc = crtc;
  1090. state.fb = fb;
  1091. /* sample coordinates in 16.16 fixed point */
  1092. state.src.x1 = src_x;
  1093. state.src.x2 = src_x + src_w;
  1094. state.src.y1 = src_y;
  1095. state.src.y2 = src_y + src_h;
  1096. /* integer pixels */
  1097. state.dst.x1 = crtc_x;
  1098. state.dst.x2 = crtc_x + crtc_w;
  1099. state.dst.y1 = crtc_y;
  1100. state.dst.y2 = crtc_y + crtc_h;
  1101. state.clip.x1 = 0;
  1102. state.clip.y1 = 0;
  1103. state.clip.x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0;
  1104. state.clip.y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0;
  1105. state.orig_src = state.src;
  1106. state.orig_dst = state.dst;
  1107. ret = intel_check_sprite_plane(plane, &state);
  1108. if (ret)
  1109. return ret;
  1110. return intel_commit_sprite_plane(plane, &state);
  1111. }
  1112. static int
  1113. intel_disable_plane(struct drm_plane *plane)
  1114. {
  1115. struct drm_device *dev = plane->dev;
  1116. struct intel_plane *intel_plane = to_intel_plane(plane);
  1117. struct intel_crtc *intel_crtc;
  1118. enum pipe pipe;
  1119. if (!plane->fb)
  1120. return 0;
  1121. if (WARN_ON(!plane->crtc))
  1122. return -EINVAL;
  1123. intel_crtc = to_intel_crtc(plane->crtc);
  1124. pipe = intel_crtc->pipe;
  1125. if (intel_crtc->active) {
  1126. bool primary_was_enabled = intel_crtc->primary_enabled;
  1127. intel_crtc->primary_enabled = true;
  1128. intel_plane->disable_plane(plane, plane->crtc);
  1129. if (!primary_was_enabled && intel_crtc->primary_enabled)
  1130. intel_post_enable_primary(plane->crtc);
  1131. }
  1132. if (intel_plane->obj) {
  1133. if (intel_crtc->active)
  1134. intel_wait_for_vblank(dev, intel_plane->pipe);
  1135. mutex_lock(&dev->struct_mutex);
  1136. intel_unpin_fb_obj(intel_plane->obj);
  1137. i915_gem_track_fb(intel_plane->obj, NULL,
  1138. INTEL_FRONTBUFFER_SPRITE(pipe));
  1139. mutex_unlock(&dev->struct_mutex);
  1140. intel_plane->obj = NULL;
  1141. }
  1142. return 0;
  1143. }
  1144. static void intel_destroy_plane(struct drm_plane *plane)
  1145. {
  1146. struct intel_plane *intel_plane = to_intel_plane(plane);
  1147. intel_disable_plane(plane);
  1148. drm_plane_cleanup(plane);
  1149. kfree(intel_plane);
  1150. }
  1151. int intel_sprite_set_colorkey(struct drm_device *dev, void *data,
  1152. struct drm_file *file_priv)
  1153. {
  1154. struct drm_intel_sprite_colorkey *set = data;
  1155. struct drm_plane *plane;
  1156. struct intel_plane *intel_plane;
  1157. int ret = 0;
  1158. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1159. return -ENODEV;
  1160. /* Make sure we don't try to enable both src & dest simultaneously */
  1161. if ((set->flags & (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE)) == (I915_SET_COLORKEY_DESTINATION | I915_SET_COLORKEY_SOURCE))
  1162. return -EINVAL;
  1163. drm_modeset_lock_all(dev);
  1164. plane = drm_plane_find(dev, set->plane_id);
  1165. if (!plane) {
  1166. ret = -ENOENT;
  1167. goto out_unlock;
  1168. }
  1169. intel_plane = to_intel_plane(plane);
  1170. ret = intel_plane->update_colorkey(plane, set);
  1171. out_unlock:
  1172. drm_modeset_unlock_all(dev);
  1173. return ret;
  1174. }
  1175. int intel_sprite_get_colorkey(struct drm_device *dev, void *data,
  1176. struct drm_file *file_priv)
  1177. {
  1178. struct drm_intel_sprite_colorkey *get = data;
  1179. struct drm_plane *plane;
  1180. struct intel_plane *intel_plane;
  1181. int ret = 0;
  1182. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1183. return -ENODEV;
  1184. drm_modeset_lock_all(dev);
  1185. plane = drm_plane_find(dev, get->plane_id);
  1186. if (!plane) {
  1187. ret = -ENOENT;
  1188. goto out_unlock;
  1189. }
  1190. intel_plane = to_intel_plane(plane);
  1191. intel_plane->get_colorkey(plane, get);
  1192. out_unlock:
  1193. drm_modeset_unlock_all(dev);
  1194. return ret;
  1195. }
  1196. int intel_plane_set_property(struct drm_plane *plane,
  1197. struct drm_property *prop,
  1198. uint64_t val)
  1199. {
  1200. struct drm_device *dev = plane->dev;
  1201. struct intel_plane *intel_plane = to_intel_plane(plane);
  1202. uint64_t old_val;
  1203. int ret = -ENOENT;
  1204. if (prop == dev->mode_config.rotation_property) {
  1205. /* exactly one rotation angle please */
  1206. if (hweight32(val & 0xf) != 1)
  1207. return -EINVAL;
  1208. if (intel_plane->rotation == val)
  1209. return 0;
  1210. old_val = intel_plane->rotation;
  1211. intel_plane->rotation = val;
  1212. ret = intel_plane_restore(plane);
  1213. if (ret)
  1214. intel_plane->rotation = old_val;
  1215. }
  1216. return ret;
  1217. }
  1218. int intel_plane_restore(struct drm_plane *plane)
  1219. {
  1220. struct intel_plane *intel_plane = to_intel_plane(plane);
  1221. if (!plane->crtc || !plane->fb)
  1222. return 0;
  1223. return plane->funcs->update_plane(plane, plane->crtc, plane->fb,
  1224. intel_plane->crtc_x, intel_plane->crtc_y,
  1225. intel_plane->crtc_w, intel_plane->crtc_h,
  1226. intel_plane->src_x, intel_plane->src_y,
  1227. intel_plane->src_w, intel_plane->src_h);
  1228. }
  1229. void intel_plane_disable(struct drm_plane *plane)
  1230. {
  1231. if (!plane->crtc || !plane->fb)
  1232. return;
  1233. intel_disable_plane(plane);
  1234. }
  1235. static const struct drm_plane_funcs intel_plane_funcs = {
  1236. .update_plane = intel_update_plane,
  1237. .disable_plane = intel_disable_plane,
  1238. .destroy = intel_destroy_plane,
  1239. .set_property = intel_plane_set_property,
  1240. };
  1241. static uint32_t ilk_plane_formats[] = {
  1242. DRM_FORMAT_XRGB8888,
  1243. DRM_FORMAT_YUYV,
  1244. DRM_FORMAT_YVYU,
  1245. DRM_FORMAT_UYVY,
  1246. DRM_FORMAT_VYUY,
  1247. };
  1248. static uint32_t snb_plane_formats[] = {
  1249. DRM_FORMAT_XBGR8888,
  1250. DRM_FORMAT_XRGB8888,
  1251. DRM_FORMAT_YUYV,
  1252. DRM_FORMAT_YVYU,
  1253. DRM_FORMAT_UYVY,
  1254. DRM_FORMAT_VYUY,
  1255. };
  1256. static uint32_t vlv_plane_formats[] = {
  1257. DRM_FORMAT_RGB565,
  1258. DRM_FORMAT_ABGR8888,
  1259. DRM_FORMAT_ARGB8888,
  1260. DRM_FORMAT_XBGR8888,
  1261. DRM_FORMAT_XRGB8888,
  1262. DRM_FORMAT_XBGR2101010,
  1263. DRM_FORMAT_ABGR2101010,
  1264. DRM_FORMAT_YUYV,
  1265. DRM_FORMAT_YVYU,
  1266. DRM_FORMAT_UYVY,
  1267. DRM_FORMAT_VYUY,
  1268. };
  1269. static uint32_t skl_plane_formats[] = {
  1270. DRM_FORMAT_RGB565,
  1271. DRM_FORMAT_ABGR8888,
  1272. DRM_FORMAT_ARGB8888,
  1273. DRM_FORMAT_XBGR8888,
  1274. DRM_FORMAT_XRGB8888,
  1275. DRM_FORMAT_YUYV,
  1276. DRM_FORMAT_YVYU,
  1277. DRM_FORMAT_UYVY,
  1278. DRM_FORMAT_VYUY,
  1279. };
  1280. int
  1281. intel_plane_init(struct drm_device *dev, enum pipe pipe, int plane)
  1282. {
  1283. struct intel_plane *intel_plane;
  1284. unsigned long possible_crtcs;
  1285. const uint32_t *plane_formats;
  1286. int num_plane_formats;
  1287. int ret;
  1288. if (INTEL_INFO(dev)->gen < 5)
  1289. return -ENODEV;
  1290. intel_plane = kzalloc(sizeof(*intel_plane), GFP_KERNEL);
  1291. if (!intel_plane)
  1292. return -ENOMEM;
  1293. switch (INTEL_INFO(dev)->gen) {
  1294. case 5:
  1295. case 6:
  1296. intel_plane->can_scale = true;
  1297. intel_plane->max_downscale = 16;
  1298. intel_plane->update_plane = ilk_update_plane;
  1299. intel_plane->disable_plane = ilk_disable_plane;
  1300. intel_plane->update_colorkey = ilk_update_colorkey;
  1301. intel_plane->get_colorkey = ilk_get_colorkey;
  1302. if (IS_GEN6(dev)) {
  1303. plane_formats = snb_plane_formats;
  1304. num_plane_formats = ARRAY_SIZE(snb_plane_formats);
  1305. } else {
  1306. plane_formats = ilk_plane_formats;
  1307. num_plane_formats = ARRAY_SIZE(ilk_plane_formats);
  1308. }
  1309. break;
  1310. case 7:
  1311. case 8:
  1312. if (IS_IVYBRIDGE(dev)) {
  1313. intel_plane->can_scale = true;
  1314. intel_plane->max_downscale = 2;
  1315. } else {
  1316. intel_plane->can_scale = false;
  1317. intel_plane->max_downscale = 1;
  1318. }
  1319. if (IS_VALLEYVIEW(dev)) {
  1320. intel_plane->update_plane = vlv_update_plane;
  1321. intel_plane->disable_plane = vlv_disable_plane;
  1322. intel_plane->update_colorkey = vlv_update_colorkey;
  1323. intel_plane->get_colorkey = vlv_get_colorkey;
  1324. plane_formats = vlv_plane_formats;
  1325. num_plane_formats = ARRAY_SIZE(vlv_plane_formats);
  1326. } else {
  1327. intel_plane->update_plane = ivb_update_plane;
  1328. intel_plane->disable_plane = ivb_disable_plane;
  1329. intel_plane->update_colorkey = ivb_update_colorkey;
  1330. intel_plane->get_colorkey = ivb_get_colorkey;
  1331. plane_formats = snb_plane_formats;
  1332. num_plane_formats = ARRAY_SIZE(snb_plane_formats);
  1333. }
  1334. break;
  1335. case 9:
  1336. /*
  1337. * FIXME: Skylake planes can be scaled (with some restrictions),
  1338. * but this is for another time.
  1339. */
  1340. intel_plane->can_scale = false;
  1341. intel_plane->max_downscale = 1;
  1342. intel_plane->update_plane = skl_update_plane;
  1343. intel_plane->disable_plane = skl_disable_plane;
  1344. intel_plane->update_colorkey = skl_update_colorkey;
  1345. intel_plane->get_colorkey = skl_get_colorkey;
  1346. plane_formats = skl_plane_formats;
  1347. num_plane_formats = ARRAY_SIZE(skl_plane_formats);
  1348. break;
  1349. default:
  1350. kfree(intel_plane);
  1351. return -ENODEV;
  1352. }
  1353. intel_plane->pipe = pipe;
  1354. intel_plane->plane = plane;
  1355. intel_plane->rotation = BIT(DRM_ROTATE_0);
  1356. possible_crtcs = (1 << pipe);
  1357. ret = drm_universal_plane_init(dev, &intel_plane->base, possible_crtcs,
  1358. &intel_plane_funcs,
  1359. plane_formats, num_plane_formats,
  1360. DRM_PLANE_TYPE_OVERLAY);
  1361. if (ret) {
  1362. kfree(intel_plane);
  1363. goto out;
  1364. }
  1365. if (!dev->mode_config.rotation_property)
  1366. dev->mode_config.rotation_property =
  1367. drm_mode_create_rotation_property(dev,
  1368. BIT(DRM_ROTATE_0) |
  1369. BIT(DRM_ROTATE_180));
  1370. if (dev->mode_config.rotation_property)
  1371. drm_object_attach_property(&intel_plane->base.base,
  1372. dev->mode_config.rotation_property,
  1373. intel_plane->rotation);
  1374. out:
  1375. return ret;
  1376. }