omap_fb.c 12 KB

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