omap_fb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. /* Note: DRM rotates counter-clockwise, TILER & DSS rotates clockwise */
  104. static uint32_t drm_rotation_to_tiler(unsigned int drm_rot)
  105. {
  106. uint32_t orient;
  107. switch (drm_rot & DRM_MODE_ROTATE_MASK) {
  108. default:
  109. case DRM_MODE_ROTATE_0:
  110. orient = 0;
  111. break;
  112. case DRM_MODE_ROTATE_90:
  113. orient = MASK_XY_FLIP | MASK_X_INVERT;
  114. break;
  115. case DRM_MODE_ROTATE_180:
  116. orient = MASK_X_INVERT | MASK_Y_INVERT;
  117. break;
  118. case DRM_MODE_ROTATE_270:
  119. orient = MASK_XY_FLIP | MASK_Y_INVERT;
  120. break;
  121. }
  122. if (drm_rot & DRM_MODE_REFLECT_X)
  123. orient ^= MASK_X_INVERT;
  124. if (drm_rot & DRM_MODE_REFLECT_Y)
  125. orient ^= MASK_Y_INVERT;
  126. return orient;
  127. }
  128. /* update ovl info for scanout, handles cases of multi-planar fb's, etc.
  129. */
  130. void omap_framebuffer_update_scanout(struct drm_framebuffer *fb,
  131. struct drm_plane_state *state, struct omap_overlay_info *info)
  132. {
  133. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  134. const struct drm_format_info *format = omap_fb->format;
  135. struct plane *plane = &omap_fb->planes[0];
  136. uint32_t x, y, orient = 0;
  137. info->fourcc = fb->format->format;
  138. info->pos_x = state->crtc_x;
  139. info->pos_y = state->crtc_y;
  140. info->out_width = state->crtc_w;
  141. info->out_height = state->crtc_h;
  142. info->width = state->src_w >> 16;
  143. info->height = state->src_h >> 16;
  144. /* DSS driver wants the w & h in rotated orientation */
  145. if (drm_rotation_90_or_270(state->rotation))
  146. swap(info->width, info->height);
  147. x = state->src_x >> 16;
  148. y = state->src_y >> 16;
  149. if (omap_gem_flags(plane->bo) & OMAP_BO_TILED) {
  150. uint32_t w = state->src_w >> 16;
  151. uint32_t h = state->src_h >> 16;
  152. orient = drm_rotation_to_tiler(state->rotation);
  153. /*
  154. * omap_gem_rotated_paddr() wants the x & y in tiler units.
  155. * Usually tiler unit size is the same as the pixel size, except
  156. * for YUV422 formats, for which the tiler unit size is 32 bits
  157. * and pixel size is 16 bits.
  158. */
  159. if (fb->format->format == DRM_FORMAT_UYVY ||
  160. fb->format->format == DRM_FORMAT_YUYV) {
  161. x /= 2;
  162. w /= 2;
  163. }
  164. /* adjust x,y offset for invert: */
  165. if (orient & MASK_Y_INVERT)
  166. y += h - 1;
  167. if (orient & MASK_X_INVERT)
  168. x += w - 1;
  169. /* Note: x and y are in TILER units, not pixels */
  170. omap_gem_rotated_dma_addr(plane->bo, orient, x, y,
  171. &info->paddr);
  172. info->rotation_type = OMAP_DSS_ROT_TILER;
  173. info->rotation = state->rotation ?: DRM_MODE_ROTATE_0;
  174. /* Note: stride in TILER units, not pixels */
  175. info->screen_width = omap_gem_tiled_stride(plane->bo, orient);
  176. } else {
  177. switch (state->rotation & DRM_MODE_ROTATE_MASK) {
  178. case 0:
  179. case DRM_MODE_ROTATE_0:
  180. /* OK */
  181. break;
  182. default:
  183. dev_warn(fb->dev->dev,
  184. "rotation '%d' ignored for non-tiled fb\n",
  185. state->rotation);
  186. break;
  187. }
  188. info->paddr = get_linear_addr(plane, format, 0, x, y);
  189. info->rotation_type = OMAP_DSS_ROT_NONE;
  190. info->rotation = DRM_MODE_ROTATE_0;
  191. info->screen_width = plane->pitch;
  192. }
  193. /* convert to pixels: */
  194. info->screen_width /= format->cpp[0];
  195. if (fb->format->format == DRM_FORMAT_NV12) {
  196. plane = &omap_fb->planes[1];
  197. if (info->rotation_type == OMAP_DSS_ROT_TILER) {
  198. WARN_ON(!(omap_gem_flags(plane->bo) & OMAP_BO_TILED));
  199. omap_gem_rotated_dma_addr(plane->bo, orient, x/2, y/2,
  200. &info->p_uv_addr);
  201. } else {
  202. info->p_uv_addr = get_linear_addr(plane, format, 1, x, y);
  203. }
  204. } else {
  205. info->p_uv_addr = 0;
  206. }
  207. }
  208. /* pin, prepare for scanout: */
  209. int omap_framebuffer_pin(struct drm_framebuffer *fb)
  210. {
  211. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  212. int ret, i, n = fb->format->num_planes;
  213. mutex_lock(&omap_fb->lock);
  214. if (omap_fb->pin_count > 0) {
  215. omap_fb->pin_count++;
  216. mutex_unlock(&omap_fb->lock);
  217. return 0;
  218. }
  219. for (i = 0; i < n; i++) {
  220. struct plane *plane = &omap_fb->planes[i];
  221. ret = omap_gem_pin(plane->bo, &plane->dma_addr);
  222. if (ret)
  223. goto fail;
  224. omap_gem_dma_sync_buffer(plane->bo, DMA_TO_DEVICE);
  225. }
  226. omap_fb->pin_count++;
  227. mutex_unlock(&omap_fb->lock);
  228. return 0;
  229. fail:
  230. for (i--; i >= 0; i--) {
  231. struct plane *plane = &omap_fb->planes[i];
  232. omap_gem_unpin(plane->bo);
  233. plane->dma_addr = 0;
  234. }
  235. mutex_unlock(&omap_fb->lock);
  236. return ret;
  237. }
  238. /* unpin, no longer being scanned out: */
  239. void omap_framebuffer_unpin(struct drm_framebuffer *fb)
  240. {
  241. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  242. int i, n = fb->format->num_planes;
  243. mutex_lock(&omap_fb->lock);
  244. omap_fb->pin_count--;
  245. if (omap_fb->pin_count > 0) {
  246. mutex_unlock(&omap_fb->lock);
  247. return;
  248. }
  249. for (i = 0; i < n; i++) {
  250. struct plane *plane = &omap_fb->planes[i];
  251. omap_gem_unpin(plane->bo);
  252. plane->dma_addr = 0;
  253. }
  254. mutex_unlock(&omap_fb->lock);
  255. }
  256. /* iterate thru all the connectors, returning ones that are attached
  257. * to the same fb..
  258. */
  259. struct drm_connector *omap_framebuffer_get_next_connector(
  260. struct drm_framebuffer *fb, struct drm_connector *from)
  261. {
  262. struct drm_device *dev = fb->dev;
  263. struct list_head *connector_list = &dev->mode_config.connector_list;
  264. struct drm_connector *connector = from;
  265. if (!from)
  266. return list_first_entry_or_null(connector_list, typeof(*from),
  267. head);
  268. list_for_each_entry_from(connector, connector_list, head) {
  269. if (connector != from) {
  270. struct drm_encoder *encoder = connector->encoder;
  271. struct drm_crtc *crtc = encoder ? encoder->crtc : NULL;
  272. if (crtc && crtc->primary->fb == fb)
  273. return connector;
  274. }
  275. }
  276. return NULL;
  277. }
  278. #ifdef CONFIG_DEBUG_FS
  279. void omap_framebuffer_describe(struct drm_framebuffer *fb, struct seq_file *m)
  280. {
  281. struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
  282. int i, n = fb->format->num_planes;
  283. seq_printf(m, "fb: %dx%d@%4.4s\n", fb->width, fb->height,
  284. (char *)&fb->format->format);
  285. for (i = 0; i < n; i++) {
  286. struct plane *plane = &omap_fb->planes[i];
  287. seq_printf(m, " %d: offset=%d pitch=%d, obj: ",
  288. i, plane->offset, plane->pitch);
  289. omap_gem_describe(plane->bo, m);
  290. }
  291. }
  292. #endif
  293. struct drm_framebuffer *omap_framebuffer_create(struct drm_device *dev,
  294. struct drm_file *file, const struct drm_mode_fb_cmd2 *mode_cmd)
  295. {
  296. unsigned int num_planes = drm_format_num_planes(mode_cmd->pixel_format);
  297. struct drm_gem_object *bos[4];
  298. struct drm_framebuffer *fb;
  299. int i;
  300. for (i = 0; i < num_planes; i++) {
  301. bos[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
  302. if (!bos[i]) {
  303. fb = ERR_PTR(-ENOENT);
  304. goto error;
  305. }
  306. }
  307. fb = omap_framebuffer_init(dev, mode_cmd, bos);
  308. if (IS_ERR(fb))
  309. goto error;
  310. return fb;
  311. error:
  312. while (--i >= 0)
  313. drm_gem_object_unreference_unlocked(bos[i]);
  314. return fb;
  315. }
  316. struct drm_framebuffer *omap_framebuffer_init(struct drm_device *dev,
  317. const struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos)
  318. {
  319. const struct drm_format_info *format = NULL;
  320. struct omap_framebuffer *omap_fb = NULL;
  321. struct drm_framebuffer *fb = NULL;
  322. unsigned int pitch = mode_cmd->pitches[0];
  323. int ret, i;
  324. DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%4.4s)",
  325. dev, mode_cmd, mode_cmd->width, mode_cmd->height,
  326. (char *)&mode_cmd->pixel_format);
  327. format = drm_format_info(mode_cmd->pixel_format);
  328. for (i = 0; i < ARRAY_SIZE(formats); i++) {
  329. if (formats[i] == mode_cmd->pixel_format)
  330. break;
  331. }
  332. if (!format || i == ARRAY_SIZE(formats)) {
  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. mutex_init(&omap_fb->lock);
  346. /*
  347. * The code below assumes that no format use more than two planes, and
  348. * that the two planes of multiplane formats need the same number of
  349. * bytes per pixel.
  350. */
  351. if (format->num_planes == 2 && pitch != mode_cmd->pitches[1]) {
  352. dev_dbg(dev->dev, "pitches differ between planes 0 and 1\n");
  353. ret = -EINVAL;
  354. goto fail;
  355. }
  356. if (pitch % format->cpp[0]) {
  357. dev_dbg(dev->dev,
  358. "buffer pitch (%u bytes) is not a multiple of pixel size (%u bytes)\n",
  359. pitch, format->cpp[0]);
  360. ret = -EINVAL;
  361. goto fail;
  362. }
  363. for (i = 0; i < format->num_planes; i++) {
  364. struct plane *plane = &omap_fb->planes[i];
  365. unsigned int vsub = i == 0 ? 1 : format->vsub;
  366. unsigned int size;
  367. size = pitch * mode_cmd->height / vsub;
  368. if (size > omap_gem_mmap_size(bos[i]) - mode_cmd->offsets[i]) {
  369. dev_dbg(dev->dev,
  370. "provided buffer object is too small! %zu < %d\n",
  371. bos[i]->size - mode_cmd->offsets[i], size);
  372. ret = -EINVAL;
  373. goto fail;
  374. }
  375. plane->bo = bos[i];
  376. plane->offset = mode_cmd->offsets[i];
  377. plane->pitch = pitch;
  378. plane->dma_addr = 0;
  379. }
  380. drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
  381. ret = drm_framebuffer_init(dev, fb, &omap_framebuffer_funcs);
  382. if (ret) {
  383. dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
  384. goto fail;
  385. }
  386. DBG("create: FB ID: %d (%p)", fb->base.id, fb);
  387. return fb;
  388. fail:
  389. kfree(omap_fb);
  390. return ERR_PTR(ret);
  391. }