omap_fb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. /* DSS to DRM formats mapping */
  28. static const struct {
  29. u32 dss_format;
  30. uint32_t pixel_format;
  31. } formats[] = {
  32. /* 16bpp [A]RGB: */
  33. { OMAP_DSS_COLOR_RGB16, DRM_FORMAT_RGB565 }, /* RGB16-565 */
  34. { OMAP_DSS_COLOR_RGB12U, DRM_FORMAT_RGBX4444 }, /* RGB12x-4444 */
  35. { OMAP_DSS_COLOR_RGBX16, DRM_FORMAT_XRGB4444 }, /* xRGB12-4444 */
  36. { OMAP_DSS_COLOR_RGBA16, DRM_FORMAT_RGBA4444 }, /* RGBA12-4444 */
  37. { OMAP_DSS_COLOR_ARGB16, DRM_FORMAT_ARGB4444 }, /* ARGB16-4444 */
  38. { OMAP_DSS_COLOR_XRGB16_1555, DRM_FORMAT_XRGB1555 }, /* xRGB15-1555 */
  39. { OMAP_DSS_COLOR_ARGB16_1555, DRM_FORMAT_ARGB1555 }, /* ARGB16-1555 */
  40. /* 24bpp RGB: */
  41. { OMAP_DSS_COLOR_RGB24P, DRM_FORMAT_RGB888 }, /* RGB24-888 */
  42. /* 32bpp [A]RGB: */
  43. { OMAP_DSS_COLOR_RGBX32, DRM_FORMAT_RGBX8888 }, /* RGBx24-8888 */
  44. { OMAP_DSS_COLOR_RGB24U, DRM_FORMAT_XRGB8888 }, /* xRGB24-8888 */
  45. { OMAP_DSS_COLOR_RGBA32, DRM_FORMAT_RGBA8888 }, /* RGBA32-8888 */
  46. { OMAP_DSS_COLOR_ARGB32, DRM_FORMAT_ARGB8888 }, /* ARGB32-8888 */
  47. /* YUV: */
  48. { OMAP_DSS_COLOR_NV12, DRM_FORMAT_NV12 },
  49. { OMAP_DSS_COLOR_YUV2, DRM_FORMAT_YUYV },
  50. { OMAP_DSS_COLOR_UYVY, DRM_FORMAT_UYVY },
  51. };
  52. /* convert from overlay's pixel formats bitmask to an array of fourcc's */
  53. uint32_t omap_framebuffer_get_formats(uint32_t *pixel_formats,
  54. uint32_t max_formats, const u32 *supported_modes)
  55. {
  56. uint32_t nformats = 0;
  57. int i = 0;
  58. for (i = 0; i < ARRAY_SIZE(formats) && nformats < max_formats; i++) {
  59. unsigned int t;
  60. for (t = 0; supported_modes[t]; ++t) {
  61. if (supported_modes[t] != formats[i].dss_format)
  62. continue;
  63. pixel_formats[nformats++] = formats[i].pixel_format;
  64. break;
  65. }
  66. }
  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 dma_addr;
  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 drm_format_info *format;
  81. u32 dss_format;
  82. struct plane planes[2];
  83. /* lock for pinning (pin_count and planes.dma_addr) */
  84. struct mutex lock;
  85. };
  86. static int omap_framebuffer_create_handle(struct drm_framebuffer *fb,
  87. struct drm_file *file_priv,
  88. unsigned int *handle)
  89. {
  90. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  91. return drm_gem_handle_create(file_priv,
  92. omap_fb->planes[0].bo, handle);
  93. }
  94. static void omap_framebuffer_destroy(struct drm_framebuffer *fb)
  95. {
  96. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  97. int i, n = fb->format->num_planes;
  98. DBG("destroy: FB ID: %d (%p)", fb->base.id, fb);
  99. drm_framebuffer_cleanup(fb);
  100. for (i = 0; i < n; i++) {
  101. struct plane *plane = &omap_fb->planes[i];
  102. drm_gem_object_unreference_unlocked(plane->bo);
  103. }
  104. kfree(omap_fb);
  105. }
  106. static const struct drm_framebuffer_funcs omap_framebuffer_funcs = {
  107. .create_handle = omap_framebuffer_create_handle,
  108. .destroy = omap_framebuffer_destroy,
  109. };
  110. static uint32_t get_linear_addr(struct plane *plane,
  111. const struct drm_format_info *format, int n, int x, int y)
  112. {
  113. uint32_t offset;
  114. offset = plane->offset
  115. + (x * format->cpp[n] / (n == 0 ? 1 : format->hsub))
  116. + (y * plane->pitch / (n == 0 ? 1 : format->vsub));
  117. return plane->dma_addr + offset;
  118. }
  119. bool omap_framebuffer_supports_rotation(struct drm_framebuffer *fb)
  120. {
  121. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  122. struct plane *plane = &omap_fb->planes[0];
  123. return omap_gem_flags(plane->bo) & OMAP_BO_TILED;
  124. }
  125. /* update ovl info for scanout, handles cases of multi-planar fb's, etc.
  126. */
  127. void omap_framebuffer_update_scanout(struct drm_framebuffer *fb,
  128. struct omap_drm_window *win, struct omap_overlay_info *info)
  129. {
  130. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  131. const struct drm_format_info *format = omap_fb->format;
  132. struct plane *plane = &omap_fb->planes[0];
  133. uint32_t x, y, orient = 0;
  134. info->color_mode = omap_fb->dss_format;
  135. info->pos_x = win->crtc_x;
  136. info->pos_y = win->crtc_y;
  137. info->out_width = win->crtc_w;
  138. info->out_height = win->crtc_h;
  139. info->width = win->src_w;
  140. info->height = win->src_h;
  141. x = win->src_x;
  142. y = win->src_y;
  143. if (omap_gem_flags(plane->bo) & OMAP_BO_TILED) {
  144. uint32_t w = win->src_w;
  145. uint32_t h = win->src_h;
  146. switch (win->rotation & DRM_MODE_ROTATE_MASK) {
  147. default:
  148. dev_err(fb->dev->dev, "invalid rotation: %02x",
  149. (uint32_t)win->rotation);
  150. /* fallthru to default to no rotation */
  151. case 0:
  152. case DRM_MODE_ROTATE_0:
  153. orient = 0;
  154. break;
  155. case DRM_MODE_ROTATE_90:
  156. orient = MASK_XY_FLIP | MASK_X_INVERT;
  157. break;
  158. case DRM_MODE_ROTATE_180:
  159. orient = MASK_X_INVERT | MASK_Y_INVERT;
  160. break;
  161. case DRM_MODE_ROTATE_270:
  162. orient = MASK_XY_FLIP | MASK_Y_INVERT;
  163. break;
  164. }
  165. if (win->rotation & DRM_MODE_REFLECT_X)
  166. orient ^= MASK_X_INVERT;
  167. if (win->rotation & DRM_MODE_REFLECT_Y)
  168. orient ^= MASK_Y_INVERT;
  169. /* adjust x,y offset for flip/invert: */
  170. if (orient & MASK_XY_FLIP)
  171. swap(w, h);
  172. if (orient & MASK_Y_INVERT)
  173. y += h - 1;
  174. if (orient & MASK_X_INVERT)
  175. x += w - 1;
  176. omap_gem_rotated_dma_addr(plane->bo, orient, x, y,
  177. &info->paddr);
  178. info->rotation_type = OMAP_DSS_ROT_TILER;
  179. info->screen_width = omap_gem_tiled_stride(plane->bo, orient);
  180. } else {
  181. switch (win->rotation & DRM_MODE_ROTATE_MASK) {
  182. case 0:
  183. case DRM_MODE_ROTATE_0:
  184. /* OK */
  185. break;
  186. default:
  187. dev_warn(fb->dev->dev,
  188. "rotation '%d' ignored for non-tiled fb\n",
  189. win->rotation);
  190. win->rotation = 0;
  191. break;
  192. }
  193. info->paddr = get_linear_addr(plane, format, 0, x, y);
  194. info->rotation_type = OMAP_DSS_ROT_NONE;
  195. info->screen_width = plane->pitch;
  196. }
  197. /* convert to pixels: */
  198. info->screen_width /= format->cpp[0];
  199. if (omap_fb->dss_format == OMAP_DSS_COLOR_NV12) {
  200. plane = &omap_fb->planes[1];
  201. if (info->rotation_type == OMAP_DSS_ROT_TILER) {
  202. WARN_ON(!(omap_gem_flags(plane->bo) & OMAP_BO_TILED));
  203. omap_gem_rotated_dma_addr(plane->bo, orient, x/2, y/2,
  204. &info->p_uv_addr);
  205. } else {
  206. info->p_uv_addr = get_linear_addr(plane, format, 1, x, y);
  207. }
  208. } else {
  209. info->p_uv_addr = 0;
  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(plane->bo, &plane->dma_addr);
  226. if (ret)
  227. goto fail;
  228. omap_gem_dma_sync_buffer(plane->bo, 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(plane->bo);
  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(plane->bo);
  256. plane->dma_addr = 0;
  257. }
  258. mutex_unlock(&omap_fb->lock);
  259. }
  260. /* iterate thru all the connectors, returning ones that are attached
  261. * to the same fb..
  262. */
  263. struct drm_connector *omap_framebuffer_get_next_connector(
  264. struct drm_framebuffer *fb, struct drm_connector *from)
  265. {
  266. struct drm_device *dev = fb->dev;
  267. struct list_head *connector_list = &dev->mode_config.connector_list;
  268. struct drm_connector *connector = from;
  269. if (!from)
  270. return list_first_entry_or_null(connector_list, typeof(*from),
  271. head);
  272. list_for_each_entry_from(connector, connector_list, head) {
  273. if (connector != from) {
  274. struct drm_encoder *encoder = connector->encoder;
  275. struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
  276. if (crtc && crtc->primary->fb == fb)
  277. return connector;
  278. }
  279. }
  280. return NULL;
  281. }
  282. #ifdef CONFIG_DEBUG_FS
  283. void omap_framebuffer_describe(struct drm_framebuffer *fb, struct seq_file *m)
  284. {
  285. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  286. int i, n = fb->format->num_planes;
  287. seq_printf(m, "fb: %dx%d@%4.4s\n", fb->width, fb->height,
  288. (char *)&fb->format->format);
  289. for (i = 0; i < n; i++) {
  290. struct plane *plane = &omap_fb->planes[i];
  291. seq_printf(m, " %d: offset=%d pitch=%d, obj: ",
  292. i, plane->offset, plane->pitch);
  293. omap_gem_describe(plane->bo, m);
  294. }
  295. }
  296. #endif
  297. struct drm_framebuffer *omap_framebuffer_create(struct drm_device *dev,
  298. struct drm_file *file, const struct drm_mode_fb_cmd2 *mode_cmd)
  299. {
  300. unsigned int num_planes = drm_format_num_planes(mode_cmd->pixel_format);
  301. struct drm_gem_object *bos[4];
  302. struct drm_framebuffer *fb;
  303. int i;
  304. for (i = 0; i < num_planes; i++) {
  305. bos[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
  306. if (!bos[i]) {
  307. fb = ERR_PTR(-ENOENT);
  308. goto error;
  309. }
  310. }
  311. fb = omap_framebuffer_init(dev, mode_cmd, bos);
  312. if (IS_ERR(fb))
  313. goto error;
  314. return fb;
  315. error:
  316. while (--i > 0)
  317. drm_gem_object_unreference_unlocked(bos[i]);
  318. return fb;
  319. }
  320. struct drm_framebuffer *omap_framebuffer_init(struct drm_device *dev,
  321. const struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos)
  322. {
  323. const struct drm_format_info *format = NULL;
  324. struct omap_framebuffer *omap_fb = NULL;
  325. struct drm_framebuffer *fb = NULL;
  326. u32 dss_format = 0;
  327. unsigned int pitch = mode_cmd->pitches[0];
  328. int ret, i;
  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. format = drm_format_info(mode_cmd->pixel_format);
  333. for (i = 0; i < ARRAY_SIZE(formats); i++) {
  334. if (formats[i].pixel_format == mode_cmd->pixel_format) {
  335. dss_format = formats[i].dss_format;
  336. break;
  337. }
  338. }
  339. if (!format || !dss_format) {
  340. dev_dbg(dev->dev, "unsupported pixel format: %4.4s\n",
  341. (char *)&mode_cmd->pixel_format);
  342. ret = -EINVAL;
  343. goto fail;
  344. }
  345. omap_fb = kzalloc(sizeof(*omap_fb), GFP_KERNEL);
  346. if (!omap_fb) {
  347. ret = -ENOMEM;
  348. goto fail;
  349. }
  350. fb = &omap_fb->base;
  351. omap_fb->format = format;
  352. omap_fb->dss_format = dss_format;
  353. mutex_init(&omap_fb->lock);
  354. /*
  355. * The code below assumes that no format use more than two planes, and
  356. * that the two planes of multiplane formats need the same number of
  357. * bytes per pixel.
  358. */
  359. if (format->num_planes == 2 && pitch != mode_cmd->pitches[1]) {
  360. dev_dbg(dev->dev, "pitches differ between planes 0 and 1\n");
  361. ret = -EINVAL;
  362. goto fail;
  363. }
  364. if (pitch % format->cpp[0]) {
  365. dev_dbg(dev->dev,
  366. "buffer pitch (%u bytes) is not a multiple of pixel size (%u bytes)\n",
  367. pitch, format->cpp[0]);
  368. ret = -EINVAL;
  369. goto fail;
  370. }
  371. for (i = 0; i < format->num_planes; i++) {
  372. struct plane *plane = &omap_fb->planes[i];
  373. unsigned int vsub = i == 0 ? 1 : format->vsub;
  374. unsigned int size;
  375. size = pitch * mode_cmd->height / vsub;
  376. if (size > omap_gem_mmap_size(bos[i]) - mode_cmd->offsets[i]) {
  377. dev_dbg(dev->dev,
  378. "provided buffer object is too small! %zu < %d\n",
  379. bos[i]->size - mode_cmd->offsets[i], size);
  380. ret = -EINVAL;
  381. goto fail;
  382. }
  383. plane->bo = bos[i];
  384. plane->offset = mode_cmd->offsets[i];
  385. plane->pitch = pitch;
  386. plane->dma_addr = 0;
  387. }
  388. drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
  389. ret = drm_framebuffer_init(dev, fb, &omap_framebuffer_funcs);
  390. if (ret) {
  391. dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
  392. goto fail;
  393. }
  394. DBG("create: FB ID: %d (%p)", fb->base.id, fb);
  395. return fb;
  396. fail:
  397. kfree(omap_fb);
  398. return ERR_PTR(ret);
  399. }