omap_fb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 <linux/seq_file.h>
  20. #include <drm/drm_crtc.h>
  21. #include <drm/drm_crtc_helper.h>
  22. #include "omap_dmm_tiler.h"
  23. #include "omap_drv.h"
  24. /*
  25. * framebuffer funcs
  26. */
  27. /* per-format info: */
  28. struct format {
  29. enum omap_color_mode dss_format;
  30. uint32_t pixel_format;
  31. struct {
  32. int stride_bpp; /* this times width is stride */
  33. int sub_y; /* sub-sample in y dimension */
  34. } planes[4];
  35. bool yuv;
  36. };
  37. static const struct format formats[] = {
  38. /* 16bpp [A]RGB: */
  39. { OMAP_DSS_COLOR_RGB16, DRM_FORMAT_RGB565, {{2, 1}}, false }, /* RGB16-565 */
  40. { OMAP_DSS_COLOR_RGB12U, DRM_FORMAT_RGBX4444, {{2, 1}}, false }, /* RGB12x-4444 */
  41. { OMAP_DSS_COLOR_RGBX16, DRM_FORMAT_XRGB4444, {{2, 1}}, false }, /* xRGB12-4444 */
  42. { OMAP_DSS_COLOR_RGBA16, DRM_FORMAT_RGBA4444, {{2, 1}}, false }, /* RGBA12-4444 */
  43. { OMAP_DSS_COLOR_ARGB16, DRM_FORMAT_ARGB4444, {{2, 1}}, false }, /* ARGB16-4444 */
  44. { OMAP_DSS_COLOR_XRGB16_1555, DRM_FORMAT_XRGB1555, {{2, 1}}, false }, /* xRGB15-1555 */
  45. { OMAP_DSS_COLOR_ARGB16_1555, DRM_FORMAT_ARGB1555, {{2, 1}}, false }, /* ARGB16-1555 */
  46. /* 24bpp RGB: */
  47. { OMAP_DSS_COLOR_RGB24P, DRM_FORMAT_RGB888, {{3, 1}}, false }, /* RGB24-888 */
  48. /* 32bpp [A]RGB: */
  49. { OMAP_DSS_COLOR_RGBX32, DRM_FORMAT_RGBX8888, {{4, 1}}, false }, /* RGBx24-8888 */
  50. { OMAP_DSS_COLOR_RGB24U, DRM_FORMAT_XRGB8888, {{4, 1}}, false }, /* xRGB24-8888 */
  51. { OMAP_DSS_COLOR_RGBA32, DRM_FORMAT_RGBA8888, {{4, 1}}, false }, /* RGBA32-8888 */
  52. { OMAP_DSS_COLOR_ARGB32, DRM_FORMAT_ARGB8888, {{4, 1}}, false }, /* ARGB32-8888 */
  53. /* YUV: */
  54. { OMAP_DSS_COLOR_NV12, DRM_FORMAT_NV12, {{1, 1}, {1, 2}}, true },
  55. { OMAP_DSS_COLOR_YUV2, DRM_FORMAT_YUYV, {{2, 1}}, true },
  56. { OMAP_DSS_COLOR_UYVY, DRM_FORMAT_UYVY, {{2, 1}}, true },
  57. };
  58. /* convert from overlay's pixel formats bitmask to an array of fourcc's */
  59. uint32_t omap_framebuffer_get_formats(uint32_t *pixel_formats,
  60. uint32_t max_formats, enum omap_color_mode supported_modes)
  61. {
  62. uint32_t nformats = 0;
  63. int i = 0;
  64. for (i = 0; i < ARRAY_SIZE(formats) && nformats < max_formats; i++)
  65. if (formats[i].dss_format & supported_modes)
  66. pixel_formats[nformats++] = formats[i].pixel_format;
  67. return nformats;
  68. }
  69. /* per-plane info for the fb: */
  70. struct plane {
  71. struct drm_gem_object *bo;
  72. uint32_t pitch;
  73. uint32_t offset;
  74. dma_addr_t paddr;
  75. };
  76. #define to_omap_framebuffer(x) container_of(x, struct omap_framebuffer, base)
  77. struct omap_framebuffer {
  78. struct drm_framebuffer base;
  79. int pin_count;
  80. const struct format *format;
  81. struct plane planes[4];
  82. /* lock for pinning (pin_count and planes.paddr) */
  83. struct mutex lock;
  84. };
  85. static int omap_framebuffer_create_handle(struct drm_framebuffer *fb,
  86. struct drm_file *file_priv,
  87. unsigned int *handle)
  88. {
  89. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  90. return drm_gem_handle_create(file_priv,
  91. omap_fb->planes[0].bo, handle);
  92. }
  93. static void omap_framebuffer_destroy(struct drm_framebuffer *fb)
  94. {
  95. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  96. int i, n = drm_format_num_planes(fb->pixel_format);
  97. DBG("destroy: FB ID: %d (%p)", fb->base.id, fb);
  98. drm_framebuffer_cleanup(fb);
  99. for (i = 0; i < n; i++) {
  100. struct plane *plane = &omap_fb->planes[i];
  101. if (plane->bo)
  102. drm_gem_object_unreference_unlocked(plane->bo);
  103. }
  104. kfree(omap_fb);
  105. }
  106. static int omap_framebuffer_dirty(struct drm_framebuffer *fb,
  107. struct drm_file *file_priv, unsigned flags, unsigned color,
  108. struct drm_clip_rect *clips, unsigned num_clips)
  109. {
  110. return 0;
  111. }
  112. static const struct drm_framebuffer_funcs omap_framebuffer_funcs = {
  113. .create_handle = omap_framebuffer_create_handle,
  114. .destroy = omap_framebuffer_destroy,
  115. .dirty = omap_framebuffer_dirty,
  116. };
  117. static uint32_t get_linear_addr(struct plane *plane,
  118. const struct format *format, int n, int x, int y)
  119. {
  120. uint32_t offset;
  121. offset = plane->offset +
  122. (x * format->planes[n].stride_bpp) +
  123. (y * plane->pitch / format->planes[n].sub_y);
  124. return plane->paddr + offset;
  125. }
  126. bool omap_framebuffer_supports_rotation(struct drm_framebuffer *fb)
  127. {
  128. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  129. struct plane *plane = &omap_fb->planes[0];
  130. return omap_gem_flags(plane->bo) & OMAP_BO_TILED;
  131. }
  132. /* update ovl info for scanout, handles cases of multi-planar fb's, etc.
  133. */
  134. void omap_framebuffer_update_scanout(struct drm_framebuffer *fb,
  135. struct omap_drm_window *win, struct omap_overlay_info *info)
  136. {
  137. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  138. const struct format *format = omap_fb->format;
  139. struct plane *plane = &omap_fb->planes[0];
  140. uint32_t x, y, orient = 0;
  141. info->color_mode = format->dss_format;
  142. info->pos_x = win->crtc_x;
  143. info->pos_y = win->crtc_y;
  144. info->out_width = win->crtc_w;
  145. info->out_height = win->crtc_h;
  146. info->width = win->src_w;
  147. info->height = win->src_h;
  148. x = win->src_x;
  149. y = win->src_y;
  150. if (omap_gem_flags(plane->bo) & OMAP_BO_TILED) {
  151. uint32_t w = win->src_w;
  152. uint32_t h = win->src_h;
  153. switch (win->rotation & DRM_ROTATE_MASK) {
  154. default:
  155. dev_err(fb->dev->dev, "invalid rotation: %02x",
  156. (uint32_t)win->rotation);
  157. /* fallthru to default to no rotation */
  158. case 0:
  159. case BIT(DRM_ROTATE_0):
  160. orient = 0;
  161. break;
  162. case BIT(DRM_ROTATE_90):
  163. orient = MASK_XY_FLIP | MASK_X_INVERT;
  164. break;
  165. case BIT(DRM_ROTATE_180):
  166. orient = MASK_X_INVERT | MASK_Y_INVERT;
  167. break;
  168. case BIT(DRM_ROTATE_270):
  169. orient = MASK_XY_FLIP | MASK_Y_INVERT;
  170. break;
  171. }
  172. if (win->rotation & BIT(DRM_REFLECT_X))
  173. orient ^= MASK_X_INVERT;
  174. if (win->rotation & BIT(DRM_REFLECT_Y))
  175. orient ^= MASK_Y_INVERT;
  176. /* adjust x,y offset for flip/invert: */
  177. if (orient & MASK_XY_FLIP)
  178. swap(w, h);
  179. if (orient & MASK_Y_INVERT)
  180. y += h - 1;
  181. if (orient & MASK_X_INVERT)
  182. x += w - 1;
  183. omap_gem_rotated_paddr(plane->bo, orient, x, y, &info->paddr);
  184. info->rotation_type = OMAP_DSS_ROT_TILER;
  185. info->screen_width = omap_gem_tiled_stride(plane->bo, orient);
  186. } else {
  187. switch (win->rotation & DRM_ROTATE_MASK) {
  188. case 0:
  189. case BIT(DRM_ROTATE_0):
  190. /* OK */
  191. break;
  192. default:
  193. dev_warn(fb->dev->dev,
  194. "rotation '%d' ignored for non-tiled fb\n",
  195. win->rotation);
  196. win->rotation = 0;
  197. break;
  198. }
  199. info->paddr = get_linear_addr(plane, format, 0, x, y);
  200. info->rotation_type = OMAP_DSS_ROT_DMA;
  201. info->screen_width = plane->pitch;
  202. }
  203. /* convert to pixels: */
  204. info->screen_width /= format->planes[0].stride_bpp;
  205. if (format->dss_format == OMAP_DSS_COLOR_NV12) {
  206. plane = &omap_fb->planes[1];
  207. if (info->rotation_type == OMAP_DSS_ROT_TILER) {
  208. WARN_ON(!(omap_gem_flags(plane->bo) & OMAP_BO_TILED));
  209. omap_gem_rotated_paddr(plane->bo, orient,
  210. x/2, y/2, &info->p_uv_addr);
  211. } else {
  212. info->p_uv_addr = get_linear_addr(plane, format, 1, x, y);
  213. }
  214. } else {
  215. info->p_uv_addr = 0;
  216. }
  217. }
  218. /* pin, prepare for scanout: */
  219. int omap_framebuffer_pin(struct drm_framebuffer *fb)
  220. {
  221. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  222. int ret, i, n = drm_format_num_planes(fb->pixel_format);
  223. mutex_lock(&omap_fb->lock);
  224. if (omap_fb->pin_count > 0) {
  225. omap_fb->pin_count++;
  226. mutex_unlock(&omap_fb->lock);
  227. return 0;
  228. }
  229. for (i = 0; i < n; i++) {
  230. struct plane *plane = &omap_fb->planes[i];
  231. ret = omap_gem_get_paddr(plane->bo, &plane->paddr, true);
  232. if (ret)
  233. goto fail;
  234. omap_gem_dma_sync(plane->bo, DMA_TO_DEVICE);
  235. }
  236. omap_fb->pin_count++;
  237. mutex_unlock(&omap_fb->lock);
  238. return 0;
  239. fail:
  240. for (i--; i >= 0; i--) {
  241. struct plane *plane = &omap_fb->planes[i];
  242. omap_gem_put_paddr(plane->bo);
  243. plane->paddr = 0;
  244. }
  245. mutex_unlock(&omap_fb->lock);
  246. return ret;
  247. }
  248. /* unpin, no longer being scanned out: */
  249. void omap_framebuffer_unpin(struct drm_framebuffer *fb)
  250. {
  251. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  252. int i, n = drm_format_num_planes(fb->pixel_format);
  253. mutex_lock(&omap_fb->lock);
  254. omap_fb->pin_count--;
  255. if (omap_fb->pin_count > 0) {
  256. mutex_unlock(&omap_fb->lock);
  257. return;
  258. }
  259. for (i = 0; i < n; i++) {
  260. struct plane *plane = &omap_fb->planes[i];
  261. omap_gem_put_paddr(plane->bo);
  262. plane->paddr = 0;
  263. }
  264. mutex_unlock(&omap_fb->lock);
  265. }
  266. /* iterate thru all the connectors, returning ones that are attached
  267. * to the same fb..
  268. */
  269. struct drm_connector *omap_framebuffer_get_next_connector(
  270. struct drm_framebuffer *fb, struct drm_connector *from)
  271. {
  272. struct drm_device *dev = fb->dev;
  273. struct list_head *connector_list = &dev->mode_config.connector_list;
  274. struct drm_connector *connector = from;
  275. if (!from)
  276. return list_first_entry_or_null(connector_list, typeof(*from),
  277. head);
  278. list_for_each_entry_from(connector, connector_list, head) {
  279. if (connector != from) {
  280. struct drm_encoder *encoder = connector->encoder;
  281. struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
  282. if (crtc && crtc->primary->fb == fb)
  283. return connector;
  284. }
  285. }
  286. return NULL;
  287. }
  288. #ifdef CONFIG_DEBUG_FS
  289. void omap_framebuffer_describe(struct drm_framebuffer *fb, struct seq_file *m)
  290. {
  291. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  292. int i, n = drm_format_num_planes(fb->pixel_format);
  293. seq_printf(m, "fb: %dx%d@%4.4s\n", fb->width, fb->height,
  294. (char *)&fb->pixel_format);
  295. for (i = 0; i < n; i++) {
  296. struct plane *plane = &omap_fb->planes[i];
  297. seq_printf(m, " %d: offset=%d pitch=%d, obj: ",
  298. i, plane->offset, plane->pitch);
  299. omap_gem_describe(plane->bo, m);
  300. }
  301. }
  302. #endif
  303. struct drm_framebuffer *omap_framebuffer_create(struct drm_device *dev,
  304. struct drm_file *file, const struct drm_mode_fb_cmd2 *mode_cmd)
  305. {
  306. struct drm_gem_object *bos[4];
  307. struct drm_framebuffer *fb;
  308. int ret;
  309. ret = objects_lookup(file, mode_cmd->pixel_format,
  310. bos, mode_cmd->handles);
  311. if (ret)
  312. return ERR_PTR(ret);
  313. fb = omap_framebuffer_init(dev, mode_cmd, bos);
  314. if (IS_ERR(fb)) {
  315. int i, n = drm_format_num_planes(mode_cmd->pixel_format);
  316. for (i = 0; i < n; i++)
  317. drm_gem_object_unreference_unlocked(bos[i]);
  318. return fb;
  319. }
  320. return fb;
  321. }
  322. struct drm_framebuffer *omap_framebuffer_init(struct drm_device *dev,
  323. const struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos)
  324. {
  325. struct omap_framebuffer *omap_fb = NULL;
  326. struct drm_framebuffer *fb = NULL;
  327. const struct format *format = NULL;
  328. int ret, i, n = drm_format_num_planes(mode_cmd->pixel_format);
  329. DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%4.4s)",
  330. dev, mode_cmd, mode_cmd->width, mode_cmd->height,
  331. (char *)&mode_cmd->pixel_format);
  332. for (i = 0; i < ARRAY_SIZE(formats); i++) {
  333. if (formats[i].pixel_format == mode_cmd->pixel_format) {
  334. format = &formats[i];
  335. break;
  336. }
  337. }
  338. if (!format) {
  339. dev_err(dev->dev, "unsupported pixel format: %4.4s\n",
  340. (char *)&mode_cmd->pixel_format);
  341. ret = -EINVAL;
  342. goto fail;
  343. }
  344. omap_fb = kzalloc(sizeof(*omap_fb), GFP_KERNEL);
  345. if (!omap_fb) {
  346. ret = -ENOMEM;
  347. goto fail;
  348. }
  349. fb = &omap_fb->base;
  350. omap_fb->format = format;
  351. mutex_init(&omap_fb->lock);
  352. for (i = 0; i < n; i++) {
  353. struct plane *plane = &omap_fb->planes[i];
  354. int size, pitch = mode_cmd->pitches[i];
  355. if (pitch < (mode_cmd->width * format->planes[i].stride_bpp)) {
  356. dev_err(dev->dev, "provided buffer pitch is too small! %d < %d\n",
  357. pitch, mode_cmd->width * format->planes[i].stride_bpp);
  358. ret = -EINVAL;
  359. goto fail;
  360. }
  361. if (pitch % format->planes[i].stride_bpp != 0) {
  362. dev_err(dev->dev,
  363. "buffer pitch (%d bytes) is not a multiple of pixel size (%d bytes)\n",
  364. pitch, format->planes[i].stride_bpp);
  365. ret = -EINVAL;
  366. goto fail;
  367. }
  368. size = pitch * mode_cmd->height / format->planes[i].sub_y;
  369. if (size > (omap_gem_mmap_size(bos[i]) - mode_cmd->offsets[i])) {
  370. dev_err(dev->dev, "provided buffer object is too small! %d < %d\n",
  371. bos[i]->size - mode_cmd->offsets[i], size);
  372. ret = -EINVAL;
  373. goto fail;
  374. }
  375. if (i > 0 && pitch != mode_cmd->pitches[i - 1]) {
  376. dev_err(dev->dev,
  377. "pitches are not the same between framebuffer planes %d != %d\n",
  378. pitch, mode_cmd->pitches[i - 1]);
  379. ret = -EINVAL;
  380. goto fail;
  381. }
  382. plane->bo = bos[i];
  383. plane->offset = mode_cmd->offsets[i];
  384. plane->pitch = pitch;
  385. plane->paddr = 0;
  386. }
  387. drm_helper_mode_fill_fb_struct(fb, mode_cmd);
  388. ret = drm_framebuffer_init(dev, fb, &omap_framebuffer_funcs);
  389. if (ret) {
  390. dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
  391. goto fail;
  392. }
  393. DBG("create: FB ID: %d (%p)", fb->base.id, fb);
  394. return fb;
  395. fail:
  396. kfree(omap_fb);
  397. return ERR_PTR(ret);
  398. }