omap_fb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. enum omap_color_mode 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, enum omap_color_mode 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. if (formats[i].dss_format & supported_modes)
  60. pixel_formats[nformats++] = formats[i].pixel_format;
  61. return nformats;
  62. }
  63. /* per-plane info for the fb: */
  64. struct plane {
  65. struct drm_gem_object *bo;
  66. uint32_t pitch;
  67. uint32_t offset;
  68. dma_addr_t paddr;
  69. };
  70. #define to_omap_framebuffer(x) container_of(x, struct omap_framebuffer, base)
  71. struct omap_framebuffer {
  72. struct drm_framebuffer base;
  73. int pin_count;
  74. const struct drm_format_info *format;
  75. enum omap_color_mode dss_format;
  76. struct plane planes[2];
  77. /* lock for pinning (pin_count and planes.paddr) */
  78. struct mutex lock;
  79. };
  80. static int omap_framebuffer_create_handle(struct drm_framebuffer *fb,
  81. struct drm_file *file_priv,
  82. unsigned int *handle)
  83. {
  84. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  85. return drm_gem_handle_create(file_priv,
  86. omap_fb->planes[0].bo, handle);
  87. }
  88. static void omap_framebuffer_destroy(struct drm_framebuffer *fb)
  89. {
  90. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  91. int i, n = fb->format->num_planes;
  92. DBG("destroy: FB ID: %d (%p)", fb->base.id, fb);
  93. drm_framebuffer_cleanup(fb);
  94. for (i = 0; i < n; i++) {
  95. struct plane *plane = &omap_fb->planes[i];
  96. drm_gem_object_unreference_unlocked(plane->bo);
  97. }
  98. kfree(omap_fb);
  99. }
  100. static const struct drm_framebuffer_funcs omap_framebuffer_funcs = {
  101. .create_handle = omap_framebuffer_create_handle,
  102. .destroy = omap_framebuffer_destroy,
  103. };
  104. static uint32_t get_linear_addr(struct plane *plane,
  105. const struct drm_format_info *format, int n, int x, int y)
  106. {
  107. uint32_t offset;
  108. offset = plane->offset
  109. + (x * format->cpp[n] / (n == 0 ? 1 : format->hsub))
  110. + (y * plane->pitch / (n == 0 ? 1 : format->vsub));
  111. return plane->paddr + offset;
  112. }
  113. bool omap_framebuffer_supports_rotation(struct drm_framebuffer *fb)
  114. {
  115. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  116. struct plane *plane = &omap_fb->planes[0];
  117. return omap_gem_flags(plane->bo) & OMAP_BO_TILED;
  118. }
  119. /* update ovl info for scanout, handles cases of multi-planar fb's, etc.
  120. */
  121. void omap_framebuffer_update_scanout(struct drm_framebuffer *fb,
  122. struct omap_drm_window *win, struct omap_overlay_info *info)
  123. {
  124. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  125. const struct drm_format_info *format = omap_fb->format;
  126. struct plane *plane = &omap_fb->planes[0];
  127. uint32_t x, y, orient = 0;
  128. info->color_mode = omap_fb->dss_format;
  129. info->pos_x = win->crtc_x;
  130. info->pos_y = win->crtc_y;
  131. info->out_width = win->crtc_w;
  132. info->out_height = win->crtc_h;
  133. info->width = win->src_w;
  134. info->height = win->src_h;
  135. x = win->src_x;
  136. y = win->src_y;
  137. if (omap_gem_flags(plane->bo) & OMAP_BO_TILED) {
  138. uint32_t w = win->src_w;
  139. uint32_t h = win->src_h;
  140. switch (win->rotation & DRM_ROTATE_MASK) {
  141. default:
  142. dev_err(fb->dev->dev, "invalid rotation: %02x",
  143. (uint32_t)win->rotation);
  144. /* fallthru to default to no rotation */
  145. case 0:
  146. case DRM_ROTATE_0:
  147. orient = 0;
  148. break;
  149. case DRM_ROTATE_90:
  150. orient = MASK_XY_FLIP | MASK_X_INVERT;
  151. break;
  152. case DRM_ROTATE_180:
  153. orient = MASK_X_INVERT | MASK_Y_INVERT;
  154. break;
  155. case DRM_ROTATE_270:
  156. orient = MASK_XY_FLIP | MASK_Y_INVERT;
  157. break;
  158. }
  159. if (win->rotation & DRM_REFLECT_X)
  160. orient ^= MASK_X_INVERT;
  161. if (win->rotation & DRM_REFLECT_Y)
  162. orient ^= MASK_Y_INVERT;
  163. /* adjust x,y offset for flip/invert: */
  164. if (orient & MASK_XY_FLIP)
  165. swap(w, h);
  166. if (orient & MASK_Y_INVERT)
  167. y += h - 1;
  168. if (orient & MASK_X_INVERT)
  169. x += w - 1;
  170. omap_gem_rotated_paddr(plane->bo, orient, x, y, &info->paddr);
  171. info->rotation_type = OMAP_DSS_ROT_TILER;
  172. info->screen_width = omap_gem_tiled_stride(plane->bo, orient);
  173. } else {
  174. switch (win->rotation & DRM_ROTATE_MASK) {
  175. case 0:
  176. case DRM_ROTATE_0:
  177. /* OK */
  178. break;
  179. default:
  180. dev_warn(fb->dev->dev,
  181. "rotation '%d' ignored for non-tiled fb\n",
  182. win->rotation);
  183. win->rotation = 0;
  184. break;
  185. }
  186. info->paddr = get_linear_addr(plane, format, 0, x, y);
  187. info->rotation_type = OMAP_DSS_ROT_DMA;
  188. info->screen_width = plane->pitch;
  189. }
  190. /* convert to pixels: */
  191. info->screen_width /= format->cpp[0];
  192. if (omap_fb->dss_format == OMAP_DSS_COLOR_NV12) {
  193. plane = &omap_fb->planes[1];
  194. if (info->rotation_type == OMAP_DSS_ROT_TILER) {
  195. WARN_ON(!(omap_gem_flags(plane->bo) & OMAP_BO_TILED));
  196. omap_gem_rotated_paddr(plane->bo, orient,
  197. x/2, y/2, &info->p_uv_addr);
  198. } else {
  199. info->p_uv_addr = get_linear_addr(plane, format, 1, x, y);
  200. }
  201. } else {
  202. info->p_uv_addr = 0;
  203. }
  204. }
  205. /* pin, prepare for scanout: */
  206. int omap_framebuffer_pin(struct drm_framebuffer *fb)
  207. {
  208. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  209. int ret, i, n = fb->format->num_planes;
  210. mutex_lock(&omap_fb->lock);
  211. if (omap_fb->pin_count > 0) {
  212. omap_fb->pin_count++;
  213. mutex_unlock(&omap_fb->lock);
  214. return 0;
  215. }
  216. for (i = 0; i < n; i++) {
  217. struct plane *plane = &omap_fb->planes[i];
  218. ret = omap_gem_get_paddr(plane->bo, &plane->paddr, true);
  219. if (ret)
  220. goto fail;
  221. omap_gem_dma_sync(plane->bo, DMA_TO_DEVICE);
  222. }
  223. omap_fb->pin_count++;
  224. mutex_unlock(&omap_fb->lock);
  225. return 0;
  226. fail:
  227. for (i--; i >= 0; i--) {
  228. struct plane *plane = &omap_fb->planes[i];
  229. omap_gem_put_paddr(plane->bo);
  230. plane->paddr = 0;
  231. }
  232. mutex_unlock(&omap_fb->lock);
  233. return ret;
  234. }
  235. /* unpin, no longer being scanned out: */
  236. void omap_framebuffer_unpin(struct drm_framebuffer *fb)
  237. {
  238. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  239. int i, n = fb->format->num_planes;
  240. mutex_lock(&omap_fb->lock);
  241. omap_fb->pin_count--;
  242. if (omap_fb->pin_count > 0) {
  243. mutex_unlock(&omap_fb->lock);
  244. return;
  245. }
  246. for (i = 0; i < n; i++) {
  247. struct plane *plane = &omap_fb->planes[i];
  248. omap_gem_put_paddr(plane->bo);
  249. plane->paddr = 0;
  250. }
  251. mutex_unlock(&omap_fb->lock);
  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_or_null(connector_list, typeof(*from),
  264. head);
  265. list_for_each_entry_from(connector, connector_list, head) {
  266. if (connector != from) {
  267. struct drm_encoder *encoder = connector->encoder;
  268. struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
  269. if (crtc && crtc->primary->fb == fb)
  270. return connector;
  271. }
  272. }
  273. return NULL;
  274. }
  275. #ifdef CONFIG_DEBUG_FS
  276. void omap_framebuffer_describe(struct drm_framebuffer *fb, struct seq_file *m)
  277. {
  278. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  279. int i, n = fb->format->num_planes;
  280. seq_printf(m, "fb: %dx%d@%4.4s\n", fb->width, fb->height,
  281. (char *)&fb->format->format);
  282. for (i = 0; i < n; i++) {
  283. struct plane *plane = &omap_fb->planes[i];
  284. seq_printf(m, " %d: offset=%d pitch=%d, obj: ",
  285. i, plane->offset, plane->pitch);
  286. omap_gem_describe(plane->bo, m);
  287. }
  288. }
  289. #endif
  290. struct drm_framebuffer *omap_framebuffer_create(struct drm_device *dev,
  291. struct drm_file *file, const struct drm_mode_fb_cmd2 *mode_cmd)
  292. {
  293. unsigned int num_planes = drm_format_num_planes(mode_cmd->pixel_format);
  294. struct drm_gem_object *bos[4];
  295. struct drm_framebuffer *fb;
  296. int i;
  297. for (i = 0; i < num_planes; i++) {
  298. bos[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
  299. if (!bos[i]) {
  300. fb = ERR_PTR(-ENOENT);
  301. goto error;
  302. }
  303. }
  304. fb = omap_framebuffer_init(dev, mode_cmd, bos);
  305. if (IS_ERR(fb))
  306. goto error;
  307. return fb;
  308. error:
  309. while (--i > 0)
  310. drm_gem_object_unreference_unlocked(bos[i]);
  311. return fb;
  312. }
  313. struct drm_framebuffer *omap_framebuffer_init(struct drm_device *dev,
  314. const struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos)
  315. {
  316. const struct drm_format_info *format = NULL;
  317. struct omap_framebuffer *omap_fb = NULL;
  318. struct drm_framebuffer *fb = NULL;
  319. enum omap_color_mode dss_format = 0;
  320. unsigned int pitch = mode_cmd->pitches[0];
  321. int ret, i;
  322. DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%4.4s)",
  323. dev, mode_cmd, mode_cmd->width, mode_cmd->height,
  324. (char *)&mode_cmd->pixel_format);
  325. format = drm_format_info(mode_cmd->pixel_format);
  326. for (i = 0; i < ARRAY_SIZE(formats); i++) {
  327. if (formats[i].pixel_format == mode_cmd->pixel_format) {
  328. dss_format = formats[i].dss_format;
  329. break;
  330. }
  331. }
  332. if (!format || !dss_format) {
  333. dev_dbg(dev->dev, "unsupported pixel format: %4.4s\n",
  334. (char *)&mode_cmd->pixel_format);
  335. ret = -EINVAL;
  336. goto fail;
  337. }
  338. omap_fb = kzalloc(sizeof(*omap_fb), GFP_KERNEL);
  339. if (!omap_fb) {
  340. ret = -ENOMEM;
  341. goto fail;
  342. }
  343. fb = &omap_fb->base;
  344. omap_fb->format = format;
  345. omap_fb->dss_format = dss_format;
  346. mutex_init(&omap_fb->lock);
  347. /*
  348. * The code below assumes that no format use more than two planes, and
  349. * that the two planes of multiplane formats need the same number of
  350. * bytes per pixel.
  351. */
  352. if (format->num_planes == 2 && pitch != mode_cmd->pitches[1]) {
  353. dev_dbg(dev->dev, "pitches differ between planes 0 and 1\n");
  354. ret = -EINVAL;
  355. goto fail;
  356. }
  357. if (pitch % format->cpp[0]) {
  358. dev_dbg(dev->dev,
  359. "buffer pitch (%u bytes) is not a multiple of pixel size (%u bytes)\n",
  360. pitch, format->cpp[0]);
  361. ret = -EINVAL;
  362. goto fail;
  363. }
  364. for (i = 0; i < format->num_planes; i++) {
  365. struct plane *plane = &omap_fb->planes[i];
  366. unsigned int vsub = i == 0 ? 1 : format->vsub;
  367. unsigned int size;
  368. size = pitch * mode_cmd->height / vsub;
  369. if (size > omap_gem_mmap_size(bos[i]) - mode_cmd->offsets[i]) {
  370. dev_dbg(dev->dev,
  371. "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(dev, 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. kfree(omap_fb);
  391. return ERR_PTR(ret);
  392. }