omap_fb.c 10 KB

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