omap_fb.c 11 KB

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