omap_fb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2011 Texas Instruments Incorporated - https://www.ti.com/
  4. * Author: Rob Clark <rob@ti.com>
  5. */
  6. #include <linux/dma-mapping.h>
  7. #include <linux/seq_file.h>
  8. #include <drm/drm_blend.h>
  9. #include <drm/drm_modeset_helper.h>
  10. #include <drm/drm_fourcc.h>
  11. #include <drm/drm_framebuffer.h>
  12. #include <drm/drm_gem_framebuffer_helper.h>
  13. #include <drm/drm_print.h>
  14. #include "omap_dmm_tiler.h"
  15. #include "omap_drv.h"
  16. /*
  17. * framebuffer funcs
  18. */
  19. static const u32 formats[] = {
  20. /* 16bpp [A]RGB: */
  21. DRM_FORMAT_RGB565, /* RGB16-565 */
  22. DRM_FORMAT_RGBX4444, /* RGB12x-4444 */
  23. DRM_FORMAT_XRGB4444, /* xRGB12-4444 */
  24. DRM_FORMAT_RGBA4444, /* RGBA12-4444 */
  25. DRM_FORMAT_ARGB4444, /* ARGB16-4444 */
  26. DRM_FORMAT_XRGB1555, /* xRGB15-1555 */
  27. DRM_FORMAT_ARGB1555, /* ARGB16-1555 */
  28. /* 24bpp RGB: */
  29. DRM_FORMAT_RGB888, /* RGB24-888 */
  30. /* 32bpp [A]RGB: */
  31. DRM_FORMAT_RGBX8888, /* RGBx24-8888 */
  32. DRM_FORMAT_XRGB8888, /* xRGB24-8888 */
  33. DRM_FORMAT_RGBA8888, /* RGBA32-8888 */
  34. DRM_FORMAT_ARGB8888, /* ARGB32-8888 */
  35. /* YUV: */
  36. DRM_FORMAT_NV12,
  37. DRM_FORMAT_YUYV,
  38. DRM_FORMAT_UYVY,
  39. };
  40. /* per-plane info for the fb: */
  41. struct plane {
  42. dma_addr_t dma_addr;
  43. };
  44. #define to_omap_framebuffer(x) container_of(x, struct omap_framebuffer, base)
  45. struct omap_framebuffer {
  46. struct drm_framebuffer base;
  47. int pin_count;
  48. const struct drm_format_info *format;
  49. struct plane planes[2];
  50. /* lock for pinning (pin_count and planes.dma_addr) */
  51. struct mutex lock;
  52. };
  53. static int omap_framebuffer_dirty(struct drm_framebuffer *fb,
  54. struct drm_file *file_priv,
  55. unsigned flags, unsigned color,
  56. struct drm_clip_rect *clips,
  57. unsigned num_clips)
  58. {
  59. struct drm_crtc *crtc;
  60. drm_modeset_lock_all(fb->dev);
  61. drm_for_each_crtc(crtc, fb->dev)
  62. omap_crtc_flush(crtc);
  63. drm_modeset_unlock_all(fb->dev);
  64. return 0;
  65. }
  66. static const struct drm_framebuffer_funcs omap_framebuffer_funcs = {
  67. .create_handle = drm_gem_fb_create_handle,
  68. .dirty = omap_framebuffer_dirty,
  69. .destroy = drm_gem_fb_destroy,
  70. };
  71. static u32 get_linear_addr(struct drm_framebuffer *fb,
  72. const struct drm_format_info *format, int n, int x, int y)
  73. {
  74. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  75. struct plane *plane = &omap_fb->planes[n];
  76. u32 offset;
  77. offset = fb->offsets[n]
  78. + (x * format->cpp[n] / (n == 0 ? 1 : format->hsub))
  79. + (y * fb->pitches[n] / (n == 0 ? 1 : format->vsub));
  80. return plane->dma_addr + offset;
  81. }
  82. bool omap_framebuffer_supports_rotation(struct drm_framebuffer *fb)
  83. {
  84. return omap_gem_flags(fb->obj[0]) & OMAP_BO_TILED_MASK;
  85. }
  86. /* Note: DRM rotates counter-clockwise, TILER & DSS rotates clockwise */
  87. static u32 drm_rotation_to_tiler(unsigned int drm_rot)
  88. {
  89. u32 orient;
  90. switch (drm_rot & DRM_MODE_ROTATE_MASK) {
  91. default:
  92. case DRM_MODE_ROTATE_0:
  93. orient = 0;
  94. break;
  95. case DRM_MODE_ROTATE_90:
  96. orient = MASK_XY_FLIP | MASK_X_INVERT;
  97. break;
  98. case DRM_MODE_ROTATE_180:
  99. orient = MASK_X_INVERT | MASK_Y_INVERT;
  100. break;
  101. case DRM_MODE_ROTATE_270:
  102. orient = MASK_XY_FLIP | MASK_Y_INVERT;
  103. break;
  104. }
  105. if (drm_rot & DRM_MODE_REFLECT_X)
  106. orient ^= MASK_X_INVERT;
  107. if (drm_rot & DRM_MODE_REFLECT_Y)
  108. orient ^= MASK_Y_INVERT;
  109. return orient;
  110. }
  111. /* update ovl info for scanout, handles cases of multi-planar fb's, etc.
  112. */
  113. void omap_framebuffer_update_scanout(struct drm_framebuffer *fb,
  114. struct drm_plane_state *state,
  115. struct omap_overlay_info *info,
  116. struct omap_overlay_info *r_info)
  117. {
  118. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  119. const struct drm_format_info *format = omap_fb->format;
  120. u32 x, y, orient = 0;
  121. info->fourcc = fb->format->format;
  122. info->pos_x = state->crtc_x;
  123. info->pos_y = state->crtc_y;
  124. info->out_width = state->crtc_w;
  125. info->out_height = state->crtc_h;
  126. info->width = state->src_w >> 16;
  127. info->height = state->src_h >> 16;
  128. /* DSS driver wants the w & h in rotated orientation */
  129. if (drm_rotation_90_or_270(state->rotation))
  130. swap(info->width, info->height);
  131. x = state->src_x >> 16;
  132. y = state->src_y >> 16;
  133. if (omap_gem_flags(fb->obj[0]) & OMAP_BO_TILED_MASK) {
  134. u32 w = state->src_w >> 16;
  135. u32 h = state->src_h >> 16;
  136. orient = drm_rotation_to_tiler(state->rotation);
  137. /*
  138. * omap_gem_rotated_paddr() wants the x & y in tiler units.
  139. * Usually tiler unit size is the same as the pixel size, except
  140. * for YUV422 formats, for which the tiler unit size is 32 bits
  141. * and pixel size is 16 bits.
  142. */
  143. if (fb->format->format == DRM_FORMAT_UYVY ||
  144. fb->format->format == DRM_FORMAT_YUYV) {
  145. x /= 2;
  146. w /= 2;
  147. }
  148. /* adjust x,y offset for invert: */
  149. if (orient & MASK_Y_INVERT)
  150. y += h - 1;
  151. if (orient & MASK_X_INVERT)
  152. x += w - 1;
  153. /* Note: x and y are in TILER units, not pixels */
  154. omap_gem_rotated_dma_addr(fb->obj[0], orient, x, y,
  155. &info->paddr);
  156. info->rotation_type = OMAP_DSS_ROT_TILER;
  157. info->rotation = state->rotation ?: DRM_MODE_ROTATE_0;
  158. /* Note: stride in TILER units, not pixels */
  159. info->screen_width = omap_gem_tiled_stride(fb->obj[0], orient);
  160. } else {
  161. switch (state->rotation & DRM_MODE_ROTATE_MASK) {
  162. case 0:
  163. case DRM_MODE_ROTATE_0:
  164. /* OK */
  165. break;
  166. default:
  167. dev_warn(fb->dev->dev,
  168. "rotation '%d' ignored for non-tiled fb\n",
  169. state->rotation);
  170. break;
  171. }
  172. info->paddr = get_linear_addr(fb, format, 0, x, y);
  173. info->rotation_type = OMAP_DSS_ROT_NONE;
  174. info->rotation = DRM_MODE_ROTATE_0;
  175. info->screen_width = fb->pitches[0];
  176. }
  177. /* convert to pixels: */
  178. info->screen_width /= format->cpp[0];
  179. if (fb->format->format == DRM_FORMAT_NV12) {
  180. if (info->rotation_type == OMAP_DSS_ROT_TILER) {
  181. WARN_ON(!(omap_gem_flags(fb->obj[1]) & OMAP_BO_TILED_MASK));
  182. omap_gem_rotated_dma_addr(fb->obj[1], orient, x/2, y/2,
  183. &info->p_uv_addr);
  184. } else {
  185. info->p_uv_addr = get_linear_addr(fb, format, 1, x, y);
  186. }
  187. } else {
  188. info->p_uv_addr = 0;
  189. }
  190. if (r_info) {
  191. info->width /= 2;
  192. info->out_width /= 2;
  193. *r_info = *info;
  194. if (fb->format->is_yuv) {
  195. if (info->width & 1) {
  196. info->width++;
  197. r_info->width--;
  198. }
  199. if (info->out_width & 1) {
  200. info->out_width++;
  201. r_info->out_width--;
  202. }
  203. }
  204. r_info->pos_x = info->pos_x + info->out_width;
  205. r_info->paddr = get_linear_addr(fb, format, 0,
  206. x + info->width, y);
  207. if (fb->format->format == DRM_FORMAT_NV12) {
  208. r_info->p_uv_addr =
  209. get_linear_addr(fb, format, 1,
  210. x + info->width, y);
  211. }
  212. }
  213. }
  214. /* pin, prepare for scanout: */
  215. int omap_framebuffer_pin(struct drm_framebuffer *fb)
  216. {
  217. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  218. int ret, i, n = fb->format->num_planes;
  219. mutex_lock(&omap_fb->lock);
  220. if (omap_fb->pin_count > 0) {
  221. omap_fb->pin_count++;
  222. mutex_unlock(&omap_fb->lock);
  223. return 0;
  224. }
  225. for (i = 0; i < n; i++) {
  226. struct plane *plane = &omap_fb->planes[i];
  227. ret = omap_gem_pin(fb->obj[i], &plane->dma_addr);
  228. if (ret)
  229. goto fail;
  230. omap_gem_dma_sync_buffer(fb->obj[i], DMA_TO_DEVICE);
  231. }
  232. omap_fb->pin_count++;
  233. mutex_unlock(&omap_fb->lock);
  234. return 0;
  235. fail:
  236. for (i--; i >= 0; i--) {
  237. struct plane *plane = &omap_fb->planes[i];
  238. omap_gem_unpin(fb->obj[i]);
  239. plane->dma_addr = 0;
  240. }
  241. mutex_unlock(&omap_fb->lock);
  242. return ret;
  243. }
  244. /* unpin, no longer being scanned out: */
  245. void omap_framebuffer_unpin(struct drm_framebuffer *fb)
  246. {
  247. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  248. int i, n = fb->format->num_planes;
  249. mutex_lock(&omap_fb->lock);
  250. omap_fb->pin_count--;
  251. if (omap_fb->pin_count > 0) {
  252. mutex_unlock(&omap_fb->lock);
  253. return;
  254. }
  255. for (i = 0; i < n; i++) {
  256. struct plane *plane = &omap_fb->planes[i];
  257. omap_gem_unpin(fb->obj[i]);
  258. plane->dma_addr = 0;
  259. }
  260. mutex_unlock(&omap_fb->lock);
  261. }
  262. #ifdef CONFIG_DEBUG_FS
  263. void omap_framebuffer_describe(struct drm_framebuffer *fb, struct seq_file *m)
  264. {
  265. int i, n = fb->format->num_planes;
  266. seq_printf(m, "fb: %dx%d@%4.4s\n", fb->width, fb->height,
  267. (char *)&fb->format->format);
  268. for (i = 0; i < n; i++) {
  269. seq_printf(m, " %d: offset=%d pitch=%d, obj: ",
  270. i, fb->offsets[n], fb->pitches[i]);
  271. omap_gem_describe(fb->obj[i], m);
  272. }
  273. }
  274. #endif
  275. struct drm_framebuffer *omap_framebuffer_create(struct drm_device *dev,
  276. struct drm_file *file, const struct drm_format_info *info,
  277. const struct drm_mode_fb_cmd2 *mode_cmd)
  278. {
  279. unsigned int num_planes = info->num_planes;
  280. struct drm_gem_object *bos[4];
  281. struct drm_framebuffer *fb;
  282. int i;
  283. for (i = 0; i < num_planes; i++) {
  284. bos[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
  285. if (!bos[i]) {
  286. fb = ERR_PTR(-ENOENT);
  287. goto error;
  288. }
  289. }
  290. fb = omap_framebuffer_init(dev, info, mode_cmd, bos);
  291. if (IS_ERR(fb))
  292. goto error;
  293. return fb;
  294. error:
  295. while (--i >= 0)
  296. drm_gem_object_put(bos[i]);
  297. return fb;
  298. }
  299. struct drm_framebuffer *omap_framebuffer_init(struct drm_device *dev,
  300. const struct drm_format_info *info,
  301. const struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos)
  302. {
  303. struct omap_framebuffer *omap_fb = NULL;
  304. struct drm_framebuffer *fb = NULL;
  305. unsigned int pitch = mode_cmd->pitches[0];
  306. int ret, i;
  307. DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%4.4s)",
  308. dev, mode_cmd, mode_cmd->width, mode_cmd->height,
  309. (char *)&mode_cmd->pixel_format);
  310. for (i = 0; i < ARRAY_SIZE(formats); i++) {
  311. if (formats[i] == mode_cmd->pixel_format)
  312. break;
  313. }
  314. if (i == ARRAY_SIZE(formats)) {
  315. dev_dbg(dev->dev, "unsupported pixel format: %4.4s\n",
  316. (char *)&mode_cmd->pixel_format);
  317. ret = -EINVAL;
  318. goto fail;
  319. }
  320. omap_fb = kzalloc_obj(*omap_fb);
  321. if (!omap_fb) {
  322. ret = -ENOMEM;
  323. goto fail;
  324. }
  325. fb = &omap_fb->base;
  326. omap_fb->format = info;
  327. mutex_init(&omap_fb->lock);
  328. /*
  329. * The code below assumes that no format use more than two planes, and
  330. * that the two planes of multiplane formats need the same number of
  331. * bytes per pixel.
  332. */
  333. if (info->num_planes == 2 && pitch != mode_cmd->pitches[1]) {
  334. dev_dbg(dev->dev, "pitches differ between planes 0 and 1\n");
  335. ret = -EINVAL;
  336. goto fail;
  337. }
  338. if (pitch % info->cpp[0]) {
  339. dev_dbg(dev->dev,
  340. "buffer pitch (%u bytes) is not a multiple of pixel size (%u bytes)\n",
  341. pitch, info->cpp[0]);
  342. ret = -EINVAL;
  343. goto fail;
  344. }
  345. for (i = 0; i < info->num_planes; i++) {
  346. struct plane *plane = &omap_fb->planes[i];
  347. unsigned int vsub = i == 0 ? 1 : info->vsub;
  348. unsigned int size;
  349. size = pitch * mode_cmd->height / vsub;
  350. if (size > omap_gem_mmap_size(bos[i]) - mode_cmd->offsets[i]) {
  351. dev_dbg(dev->dev,
  352. "provided buffer object is too small! %zu < %d\n",
  353. bos[i]->size - mode_cmd->offsets[i], size);
  354. ret = -EINVAL;
  355. goto fail;
  356. }
  357. fb->obj[i] = bos[i];
  358. plane->dma_addr = 0;
  359. }
  360. drm_helper_mode_fill_fb_struct(dev, fb, info, mode_cmd);
  361. ret = drm_framebuffer_init(dev, fb, &omap_framebuffer_funcs);
  362. if (ret) {
  363. dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
  364. goto fail;
  365. }
  366. DBG("create: FB ID: %d (%p)", fb->base.id, fb);
  367. return fb;
  368. fail:
  369. kfree(omap_fb);
  370. return ERR_PTR(ret);
  371. }