omap_fb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * drivers/gpu/drm/omapdrm/omap_fb.c
  3. *
  4. * Copyright (C) 2011 Texas Instruments
  5. * Author: Rob Clark <rob@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "omap_drv.h"
  20. #include "omap_dmm_tiler.h"
  21. #include "drm_crtc.h"
  22. #include "drm_crtc_helper.h"
  23. /*
  24. * framebuffer funcs
  25. */
  26. /* per-format info: */
  27. struct format {
  28. enum omap_color_mode dss_format;
  29. uint32_t pixel_format;
  30. struct {
  31. int stride_bpp; /* this times width is stride */
  32. int sub_y; /* sub-sample in y dimension */
  33. } planes[4];
  34. bool yuv;
  35. };
  36. static const struct format formats[] = {
  37. /* 16bpp [A]RGB: */
  38. { OMAP_DSS_COLOR_RGB16, DRM_FORMAT_RGB565, {{2, 1}}, false }, /* RGB16-565 */
  39. { OMAP_DSS_COLOR_RGB12U, DRM_FORMAT_RGBX4444, {{2, 1}}, false }, /* RGB12x-4444 */
  40. { OMAP_DSS_COLOR_RGBX16, DRM_FORMAT_XRGB4444, {{2, 1}}, false }, /* xRGB12-4444 */
  41. { OMAP_DSS_COLOR_RGBA16, DRM_FORMAT_RGBA4444, {{2, 1}}, false }, /* RGBA12-4444 */
  42. { OMAP_DSS_COLOR_ARGB16, DRM_FORMAT_ARGB4444, {{2, 1}}, false }, /* ARGB16-4444 */
  43. { OMAP_DSS_COLOR_XRGB16_1555, DRM_FORMAT_XRGB1555, {{2, 1}}, false }, /* xRGB15-1555 */
  44. { OMAP_DSS_COLOR_ARGB16_1555, DRM_FORMAT_ARGB1555, {{2, 1}}, false }, /* ARGB16-1555 */
  45. /* 24bpp RGB: */
  46. { OMAP_DSS_COLOR_RGB24P, DRM_FORMAT_RGB888, {{3, 1}}, false }, /* RGB24-888 */
  47. /* 32bpp [A]RGB: */
  48. { OMAP_DSS_COLOR_RGBX32, DRM_FORMAT_RGBX8888, {{4, 1}}, false }, /* RGBx24-8888 */
  49. { OMAP_DSS_COLOR_RGB24U, DRM_FORMAT_XRGB8888, {{4, 1}}, false }, /* xRGB24-8888 */
  50. { OMAP_DSS_COLOR_RGBA32, DRM_FORMAT_RGBA8888, {{4, 1}}, false }, /* RGBA32-8888 */
  51. { OMAP_DSS_COLOR_ARGB32, DRM_FORMAT_ARGB8888, {{4, 1}}, false }, /* ARGB32-8888 */
  52. /* YUV: */
  53. { OMAP_DSS_COLOR_NV12, DRM_FORMAT_NV12, {{1, 1}, {1, 2}}, true },
  54. { OMAP_DSS_COLOR_YUV2, DRM_FORMAT_YUYV, {{2, 1}}, true },
  55. { OMAP_DSS_COLOR_UYVY, DRM_FORMAT_UYVY, {{2, 1}}, true },
  56. };
  57. /* convert from overlay's pixel formats bitmask to an array of fourcc's */
  58. uint32_t omap_framebuffer_get_formats(uint32_t *pixel_formats,
  59. uint32_t max_formats, enum omap_color_mode supported_modes)
  60. {
  61. uint32_t nformats = 0;
  62. int i = 0;
  63. for (i = 0; i < ARRAY_SIZE(formats) && nformats < max_formats; i++)
  64. if (formats[i].dss_format & supported_modes)
  65. pixel_formats[nformats++] = formats[i].pixel_format;
  66. return nformats;
  67. }
  68. /* per-plane info for the fb: */
  69. struct plane {
  70. struct drm_gem_object *bo;
  71. uint32_t pitch;
  72. uint32_t offset;
  73. dma_addr_t paddr;
  74. };
  75. #define to_omap_framebuffer(x) container_of(x, struct omap_framebuffer, base)
  76. struct omap_framebuffer {
  77. struct drm_framebuffer base;
  78. const struct format *format;
  79. struct plane planes[4];
  80. };
  81. static int omap_framebuffer_create_handle(struct drm_framebuffer *fb,
  82. struct drm_file *file_priv,
  83. unsigned int *handle)
  84. {
  85. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  86. return drm_gem_handle_create(file_priv,
  87. omap_fb->planes[0].bo, handle);
  88. }
  89. static void omap_framebuffer_destroy(struct drm_framebuffer *fb)
  90. {
  91. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  92. int i, n = drm_format_num_planes(fb->pixel_format);
  93. DBG("destroy: FB ID: %d (%p)", fb->base.id, fb);
  94. drm_framebuffer_cleanup(fb);
  95. for (i = 0; i < n; i++) {
  96. struct plane *plane = &omap_fb->planes[i];
  97. if (plane->bo)
  98. drm_gem_object_unreference_unlocked(plane->bo);
  99. }
  100. kfree(omap_fb);
  101. }
  102. static int omap_framebuffer_dirty(struct drm_framebuffer *fb,
  103. struct drm_file *file_priv, unsigned flags, unsigned color,
  104. struct drm_clip_rect *clips, unsigned num_clips)
  105. {
  106. int i;
  107. for (i = 0; i < num_clips; i++) {
  108. omap_framebuffer_flush(fb, clips[i].x1, clips[i].y1,
  109. clips[i].x2 - clips[i].x1,
  110. clips[i].y2 - clips[i].y1);
  111. }
  112. return 0;
  113. }
  114. static const struct drm_framebuffer_funcs omap_framebuffer_funcs = {
  115. .create_handle = omap_framebuffer_create_handle,
  116. .destroy = omap_framebuffer_destroy,
  117. .dirty = omap_framebuffer_dirty,
  118. };
  119. static uint32_t get_linear_addr(struct plane *plane,
  120. const struct format *format, int n, int x, int y)
  121. {
  122. uint32_t offset;
  123. offset = plane->offset +
  124. (x * format->planes[n].stride_bpp) +
  125. (y * plane->pitch / format->planes[n].sub_y);
  126. return plane->paddr + offset;
  127. }
  128. /* update ovl info for scanout, handles cases of multi-planar fb's, etc.
  129. */
  130. void omap_framebuffer_update_scanout(struct drm_framebuffer *fb,
  131. struct omap_drm_window *win, struct omap_overlay_info *info)
  132. {
  133. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  134. const struct format *format = omap_fb->format;
  135. struct plane *plane = &omap_fb->planes[0];
  136. uint32_t x, y, orient = 0;
  137. info->color_mode = format->dss_format;
  138. info->pos_x = win->crtc_x;
  139. info->pos_y = win->crtc_y;
  140. info->out_width = win->crtc_w;
  141. info->out_height = win->crtc_h;
  142. info->width = win->src_w;
  143. info->height = win->src_h;
  144. x = win->src_x;
  145. y = win->src_y;
  146. if (omap_gem_flags(plane->bo) & OMAP_BO_TILED) {
  147. uint32_t w = win->src_w;
  148. uint32_t h = win->src_h;
  149. switch (win->rotation & 0xf) {
  150. default:
  151. dev_err(fb->dev->dev, "invalid rotation: %02x",
  152. (uint32_t)win->rotation);
  153. /* fallthru to default to no rotation */
  154. case 0:
  155. case BIT(DRM_ROTATE_0):
  156. orient = 0;
  157. break;
  158. case BIT(DRM_ROTATE_90):
  159. orient = MASK_XY_FLIP | MASK_X_INVERT;
  160. break;
  161. case BIT(DRM_ROTATE_180):
  162. orient = MASK_X_INVERT | MASK_Y_INVERT;
  163. break;
  164. case BIT(DRM_ROTATE_270):
  165. orient = MASK_XY_FLIP | MASK_Y_INVERT;
  166. break;
  167. }
  168. if (win->rotation & BIT(DRM_REFLECT_X))
  169. orient ^= MASK_X_INVERT;
  170. if (win->rotation & BIT(DRM_REFLECT_Y))
  171. orient ^= MASK_Y_INVERT;
  172. /* adjust x,y offset for flip/invert: */
  173. if (orient & MASK_XY_FLIP)
  174. swap(w, h);
  175. if (orient & MASK_Y_INVERT)
  176. y += h - 1;
  177. if (orient & MASK_X_INVERT)
  178. x += w - 1;
  179. omap_gem_rotated_paddr(plane->bo, orient, x, y, &info->paddr);
  180. info->rotation_type = OMAP_DSS_ROT_TILER;
  181. info->screen_width = omap_gem_tiled_stride(plane->bo, orient);
  182. } else {
  183. info->paddr = get_linear_addr(plane, format, 0, x, y);
  184. info->rotation_type = OMAP_DSS_ROT_DMA;
  185. info->screen_width = plane->pitch;
  186. }
  187. /* convert to pixels: */
  188. info->screen_width /= format->planes[0].stride_bpp;
  189. if (format->dss_format == OMAP_DSS_COLOR_NV12) {
  190. plane = &omap_fb->planes[1];
  191. if (info->rotation_type == OMAP_DSS_ROT_TILER) {
  192. WARN_ON(!(omap_gem_flags(plane->bo) & OMAP_BO_TILED));
  193. omap_gem_rotated_paddr(plane->bo, orient,
  194. x/2, y/2, &info->p_uv_addr);
  195. } else {
  196. info->p_uv_addr = get_linear_addr(plane, format, 1, x, y);
  197. }
  198. } else {
  199. info->p_uv_addr = 0;
  200. }
  201. }
  202. /* Call for unpin 'a' (if not NULL), and pin 'b' (if not NULL). Although
  203. * buffers to unpin are just pushed to the unpin fifo so that the
  204. * caller can defer unpin until vblank.
  205. *
  206. * Note if this fails (ie. something went very wrong!), all buffers are
  207. * unpinned, and the caller disables the overlay. We could have tried
  208. * to revert back to the previous set of pinned buffers but if things are
  209. * hosed there is no guarantee that would succeed.
  210. */
  211. int omap_framebuffer_replace(struct drm_framebuffer *a,
  212. struct drm_framebuffer *b, void *arg,
  213. void (*unpin)(void *arg, struct drm_gem_object *bo))
  214. {
  215. int ret = 0, i, na, nb;
  216. struct omap_framebuffer *ofba = to_omap_framebuffer(a);
  217. struct omap_framebuffer *ofbb = to_omap_framebuffer(b);
  218. uint32_t pinned_mask = 0;
  219. na = a ? drm_format_num_planes(a->pixel_format) : 0;
  220. nb = b ? drm_format_num_planes(b->pixel_format) : 0;
  221. for (i = 0; i < max(na, nb); i++) {
  222. struct plane *pa, *pb;
  223. pa = (i < na) ? &ofba->planes[i] : NULL;
  224. pb = (i < nb) ? &ofbb->planes[i] : NULL;
  225. if (pa)
  226. unpin(arg, pa->bo);
  227. if (pb && !ret) {
  228. ret = omap_gem_get_paddr(pb->bo, &pb->paddr, true);
  229. if (!ret) {
  230. omap_gem_dma_sync(pb->bo, DMA_TO_DEVICE);
  231. pinned_mask |= (1 << i);
  232. }
  233. }
  234. }
  235. if (ret) {
  236. /* something went wrong.. unpin what has been pinned */
  237. for (i = 0; i < nb; i++) {
  238. if (pinned_mask & (1 << i)) {
  239. struct plane *pb = &ofba->planes[i];
  240. unpin(arg, pb->bo);
  241. }
  242. }
  243. }
  244. return ret;
  245. }
  246. struct drm_gem_object *omap_framebuffer_bo(struct drm_framebuffer *fb, int p)
  247. {
  248. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  249. if (p >= drm_format_num_planes(fb->pixel_format))
  250. return NULL;
  251. return omap_fb->planes[p].bo;
  252. }
  253. /* iterate thru all the connectors, returning ones that are attached
  254. * to the same fb..
  255. */
  256. struct drm_connector *omap_framebuffer_get_next_connector(
  257. struct drm_framebuffer *fb, struct drm_connector *from)
  258. {
  259. struct drm_device *dev = fb->dev;
  260. struct list_head *connector_list = &dev->mode_config.connector_list;
  261. struct drm_connector *connector = from;
  262. if (!from)
  263. return list_first_entry(connector_list, typeof(*from), head);
  264. list_for_each_entry_from(connector, connector_list, head) {
  265. if (connector != from) {
  266. struct drm_encoder *encoder = connector->encoder;
  267. struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
  268. if (crtc && crtc->fb == fb)
  269. return connector;
  270. }
  271. }
  272. return NULL;
  273. }
  274. /* flush an area of the framebuffer (in case of manual update display that
  275. * is not automatically flushed)
  276. */
  277. void omap_framebuffer_flush(struct drm_framebuffer *fb,
  278. int x, int y, int w, int h)
  279. {
  280. struct drm_connector *connector = NULL;
  281. VERB("flush: %d,%d %dx%d, fb=%p", x, y, w, h, fb);
  282. while ((connector = omap_framebuffer_get_next_connector(fb, connector))) {
  283. /* only consider connectors that are part of a chain */
  284. if (connector->encoder && connector->encoder->crtc) {
  285. /* TODO: maybe this should propagate thru the crtc who
  286. * could do the coordinate translation..
  287. */
  288. struct drm_crtc *crtc = connector->encoder->crtc;
  289. int cx = max(0, x - crtc->x);
  290. int cy = max(0, y - crtc->y);
  291. int cw = w + (x - crtc->x) - cx;
  292. int ch = h + (y - crtc->y) - cy;
  293. omap_connector_flush(connector, cx, cy, cw, ch);
  294. }
  295. }
  296. }
  297. #ifdef CONFIG_DEBUG_FS
  298. void omap_framebuffer_describe(struct drm_framebuffer *fb, struct seq_file *m)
  299. {
  300. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  301. int i, n = drm_format_num_planes(fb->pixel_format);
  302. seq_printf(m, "fb: %dx%d@%4.4s\n", fb->width, fb->height,
  303. (char *)&fb->pixel_format);
  304. for (i = 0; i < n; i++) {
  305. struct plane *plane = &omap_fb->planes[i];
  306. seq_printf(m, " %d: offset=%d pitch=%d, obj: ",
  307. i, plane->offset, plane->pitch);
  308. omap_gem_describe(plane->bo, m);
  309. }
  310. }
  311. #endif
  312. struct drm_framebuffer *omap_framebuffer_create(struct drm_device *dev,
  313. struct drm_file *file, struct drm_mode_fb_cmd2 *mode_cmd)
  314. {
  315. struct drm_gem_object *bos[4];
  316. struct drm_framebuffer *fb;
  317. int ret;
  318. ret = objects_lookup(dev, file, mode_cmd->pixel_format,
  319. bos, mode_cmd->handles);
  320. if (ret)
  321. return ERR_PTR(ret);
  322. fb = omap_framebuffer_init(dev, mode_cmd, bos);
  323. if (IS_ERR(fb)) {
  324. int i, n = drm_format_num_planes(mode_cmd->pixel_format);
  325. for (i = 0; i < n; i++)
  326. drm_gem_object_unreference_unlocked(bos[i]);
  327. return fb;
  328. }
  329. return fb;
  330. }
  331. struct drm_framebuffer *omap_framebuffer_init(struct drm_device *dev,
  332. struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos)
  333. {
  334. struct omap_framebuffer *omap_fb;
  335. struct drm_framebuffer *fb = NULL;
  336. const struct format *format = NULL;
  337. int ret, i, n = drm_format_num_planes(mode_cmd->pixel_format);
  338. DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%4.4s)",
  339. dev, mode_cmd, mode_cmd->width, mode_cmd->height,
  340. (char *)&mode_cmd->pixel_format);
  341. for (i = 0; i < ARRAY_SIZE(formats); i++) {
  342. if (formats[i].pixel_format == mode_cmd->pixel_format) {
  343. format = &formats[i];
  344. break;
  345. }
  346. }
  347. if (!format) {
  348. dev_err(dev->dev, "unsupported pixel format: %4.4s\n",
  349. (char *)&mode_cmd->pixel_format);
  350. ret = -EINVAL;
  351. goto fail;
  352. }
  353. omap_fb = kzalloc(sizeof(*omap_fb), GFP_KERNEL);
  354. if (!omap_fb) {
  355. ret = -ENOMEM;
  356. goto fail;
  357. }
  358. fb = &omap_fb->base;
  359. omap_fb->format = format;
  360. for (i = 0; i < n; i++) {
  361. struct plane *plane = &omap_fb->planes[i];
  362. int size, pitch = mode_cmd->pitches[i];
  363. if (pitch < (mode_cmd->width * format->planes[i].stride_bpp)) {
  364. dev_err(dev->dev, "provided buffer pitch is too small! %d < %d\n",
  365. pitch, mode_cmd->width * format->planes[i].stride_bpp);
  366. ret = -EINVAL;
  367. goto fail;
  368. }
  369. size = pitch * mode_cmd->height / format->planes[i].sub_y;
  370. if (size > (omap_gem_mmap_size(bos[i]) - mode_cmd->offsets[i])) {
  371. dev_err(dev->dev, "provided buffer object is too small! %d < %d\n",
  372. bos[i]->size - mode_cmd->offsets[i], size);
  373. ret = -EINVAL;
  374. goto fail;
  375. }
  376. plane->bo = bos[i];
  377. plane->offset = mode_cmd->offsets[i];
  378. plane->pitch = pitch;
  379. plane->paddr = 0;
  380. }
  381. drm_helper_mode_fill_fb_struct(fb, mode_cmd);
  382. ret = drm_framebuffer_init(dev, fb, &omap_framebuffer_funcs);
  383. if (ret) {
  384. dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
  385. goto fail;
  386. }
  387. DBG("create: FB ID: %d (%p)", fb->base.id, fb);
  388. return fb;
  389. fail:
  390. if (fb)
  391. omap_framebuffer_destroy(fb);
  392. return ERR_PTR(ret);
  393. }