omap_fb.c 11 KB

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