omap_fb.c 11 KB

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