PageRenderTime 828ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/gpu/drm/ast/ast_fb.c

https://github.com/gby/linux
C | 366 lines | 270 code | 62 blank | 34 comment | 25 complexity | eb177eb9dfaf36a42038979933e0e6b0 MD5 | raw file
  1. /*
  2. * Copyright 2012 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sub license, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  15. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  16. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  17. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  18. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. *
  20. * The above copyright notice and this permission notice (including the
  21. * next paragraph) shall be included in all copies or substantial portions
  22. * of the Software.
  23. *
  24. */
  25. /*
  26. * Authors: Dave Airlie <airlied@redhat.com>
  27. */
  28. #include <linux/module.h>
  29. #include <linux/kernel.h>
  30. #include <linux/errno.h>
  31. #include <linux/string.h>
  32. #include <linux/mm.h>
  33. #include <linux/tty.h>
  34. #include <linux/sysrq.h>
  35. #include <linux/delay.h>
  36. #include <linux/init.h>
  37. #include <drm/drmP.h>
  38. #include <drm/drm_crtc.h>
  39. #include <drm/drm_fb_helper.h>
  40. #include <drm/drm_crtc_helper.h>
  41. #include "ast_drv.h"
  42. static void ast_dirty_update(struct ast_fbdev *afbdev,
  43. int x, int y, int width, int height)
  44. {
  45. int i;
  46. struct drm_gem_object *obj;
  47. struct ast_bo *bo;
  48. int src_offset, dst_offset;
  49. int bpp = afbdev->afb.base.format->cpp[0];
  50. int ret = -EBUSY;
  51. bool unmap = false;
  52. bool store_for_later = false;
  53. int x2, y2;
  54. unsigned long flags;
  55. obj = afbdev->afb.obj;
  56. bo = gem_to_ast_bo(obj);
  57. /*
  58. * try and reserve the BO, if we fail with busy
  59. * then the BO is being moved and we should
  60. * store up the damage until later.
  61. */
  62. if (drm_can_sleep())
  63. ret = ast_bo_reserve(bo, true);
  64. if (ret) {
  65. if (ret != -EBUSY)
  66. return;
  67. store_for_later = true;
  68. }
  69. x2 = x + width - 1;
  70. y2 = y + height - 1;
  71. spin_lock_irqsave(&afbdev->dirty_lock, flags);
  72. if (afbdev->y1 < y)
  73. y = afbdev->y1;
  74. if (afbdev->y2 > y2)
  75. y2 = afbdev->y2;
  76. if (afbdev->x1 < x)
  77. x = afbdev->x1;
  78. if (afbdev->x2 > x2)
  79. x2 = afbdev->x2;
  80. if (store_for_later) {
  81. afbdev->x1 = x;
  82. afbdev->x2 = x2;
  83. afbdev->y1 = y;
  84. afbdev->y2 = y2;
  85. spin_unlock_irqrestore(&afbdev->dirty_lock, flags);
  86. return;
  87. }
  88. afbdev->x1 = afbdev->y1 = INT_MAX;
  89. afbdev->x2 = afbdev->y2 = 0;
  90. spin_unlock_irqrestore(&afbdev->dirty_lock, flags);
  91. if (!bo->kmap.virtual) {
  92. ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
  93. if (ret) {
  94. DRM_ERROR("failed to kmap fb updates\n");
  95. ast_bo_unreserve(bo);
  96. return;
  97. }
  98. unmap = true;
  99. }
  100. for (i = y; i <= y2; i++) {
  101. /* assume equal stride for now */
  102. src_offset = dst_offset = i * afbdev->afb.base.pitches[0] + (x * bpp);
  103. memcpy_toio(bo->kmap.virtual + src_offset, afbdev->sysram + src_offset, (x2 - x + 1) * bpp);
  104. }
  105. if (unmap)
  106. ttm_bo_kunmap(&bo->kmap);
  107. ast_bo_unreserve(bo);
  108. }
  109. static void ast_fillrect(struct fb_info *info,
  110. const struct fb_fillrect *rect)
  111. {
  112. struct ast_fbdev *afbdev = info->par;
  113. drm_fb_helper_sys_fillrect(info, rect);
  114. ast_dirty_update(afbdev, rect->dx, rect->dy, rect->width,
  115. rect->height);
  116. }
  117. static void ast_copyarea(struct fb_info *info,
  118. const struct fb_copyarea *area)
  119. {
  120. struct ast_fbdev *afbdev = info->par;
  121. drm_fb_helper_sys_copyarea(info, area);
  122. ast_dirty_update(afbdev, area->dx, area->dy, area->width,
  123. area->height);
  124. }
  125. static void ast_imageblit(struct fb_info *info,
  126. const struct fb_image *image)
  127. {
  128. struct ast_fbdev *afbdev = info->par;
  129. drm_fb_helper_sys_imageblit(info, image);
  130. ast_dirty_update(afbdev, image->dx, image->dy, image->width,
  131. image->height);
  132. }
  133. static struct fb_ops astfb_ops = {
  134. .owner = THIS_MODULE,
  135. .fb_check_var = drm_fb_helper_check_var,
  136. .fb_set_par = drm_fb_helper_set_par,
  137. .fb_fillrect = ast_fillrect,
  138. .fb_copyarea = ast_copyarea,
  139. .fb_imageblit = ast_imageblit,
  140. .fb_pan_display = drm_fb_helper_pan_display,
  141. .fb_blank = drm_fb_helper_blank,
  142. .fb_setcmap = drm_fb_helper_setcmap,
  143. .fb_debug_enter = drm_fb_helper_debug_enter,
  144. .fb_debug_leave = drm_fb_helper_debug_leave,
  145. };
  146. static int astfb_create_object(struct ast_fbdev *afbdev,
  147. const struct drm_mode_fb_cmd2 *mode_cmd,
  148. struct drm_gem_object **gobj_p)
  149. {
  150. struct drm_device *dev = afbdev->helper.dev;
  151. u32 size;
  152. struct drm_gem_object *gobj;
  153. int ret = 0;
  154. size = mode_cmd->pitches[0] * mode_cmd->height;
  155. ret = ast_gem_create(dev, size, true, &gobj);
  156. if (ret)
  157. return ret;
  158. *gobj_p = gobj;
  159. return ret;
  160. }
  161. static int astfb_create(struct drm_fb_helper *helper,
  162. struct drm_fb_helper_surface_size *sizes)
  163. {
  164. struct ast_fbdev *afbdev =
  165. container_of(helper, struct ast_fbdev, helper);
  166. struct drm_device *dev = afbdev->helper.dev;
  167. struct drm_mode_fb_cmd2 mode_cmd;
  168. struct drm_framebuffer *fb;
  169. struct fb_info *info;
  170. int size, ret;
  171. void *sysram;
  172. struct drm_gem_object *gobj = NULL;
  173. struct ast_bo *bo = NULL;
  174. mode_cmd.width = sizes->surface_width;
  175. mode_cmd.height = sizes->surface_height;
  176. mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7)/8);
  177. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  178. sizes->surface_depth);
  179. size = mode_cmd.pitches[0] * mode_cmd.height;
  180. ret = astfb_create_object(afbdev, &mode_cmd, &gobj);
  181. if (ret) {
  182. DRM_ERROR("failed to create fbcon backing object %d\n", ret);
  183. return ret;
  184. }
  185. bo = gem_to_ast_bo(gobj);
  186. sysram = vmalloc(size);
  187. if (!sysram)
  188. return -ENOMEM;
  189. info = drm_fb_helper_alloc_fbi(helper);
  190. if (IS_ERR(info)) {
  191. ret = PTR_ERR(info);
  192. goto out;
  193. }
  194. info->par = afbdev;
  195. ret = ast_framebuffer_init(dev, &afbdev->afb, &mode_cmd, gobj);
  196. if (ret)
  197. goto out;
  198. afbdev->sysram = sysram;
  199. afbdev->size = size;
  200. fb = &afbdev->afb.base;
  201. afbdev->helper.fb = fb;
  202. strcpy(info->fix.id, "astdrmfb");
  203. info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
  204. info->fbops = &astfb_ops;
  205. info->apertures->ranges[0].base = pci_resource_start(dev->pdev, 0);
  206. info->apertures->ranges[0].size = pci_resource_len(dev->pdev, 0);
  207. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
  208. drm_fb_helper_fill_var(info, &afbdev->helper, sizes->fb_width, sizes->fb_height);
  209. info->screen_base = sysram;
  210. info->screen_size = size;
  211. info->pixmap.flags = FB_PIXMAP_SYSTEM;
  212. DRM_DEBUG_KMS("allocated %dx%d\n",
  213. fb->width, fb->height);
  214. return 0;
  215. out:
  216. vfree(sysram);
  217. return ret;
  218. }
  219. static void ast_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  220. u16 blue, int regno)
  221. {
  222. struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
  223. ast_crtc->lut_r[regno] = red >> 8;
  224. ast_crtc->lut_g[regno] = green >> 8;
  225. ast_crtc->lut_b[regno] = blue >> 8;
  226. }
  227. static void ast_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  228. u16 *blue, int regno)
  229. {
  230. struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
  231. *red = ast_crtc->lut_r[regno] << 8;
  232. *green = ast_crtc->lut_g[regno] << 8;
  233. *blue = ast_crtc->lut_b[regno] << 8;
  234. }
  235. static const struct drm_fb_helper_funcs ast_fb_helper_funcs = {
  236. .gamma_set = ast_fb_gamma_set,
  237. .gamma_get = ast_fb_gamma_get,
  238. .fb_probe = astfb_create,
  239. };
  240. static void ast_fbdev_destroy(struct drm_device *dev,
  241. struct ast_fbdev *afbdev)
  242. {
  243. struct ast_framebuffer *afb = &afbdev->afb;
  244. drm_fb_helper_unregister_fbi(&afbdev->helper);
  245. if (afb->obj) {
  246. drm_gem_object_unreference_unlocked(afb->obj);
  247. afb->obj = NULL;
  248. }
  249. drm_fb_helper_fini(&afbdev->helper);
  250. vfree(afbdev->sysram);
  251. drm_framebuffer_unregister_private(&afb->base);
  252. drm_framebuffer_cleanup(&afb->base);
  253. }
  254. int ast_fbdev_init(struct drm_device *dev)
  255. {
  256. struct ast_private *ast = dev->dev_private;
  257. struct ast_fbdev *afbdev;
  258. int ret;
  259. afbdev = kzalloc(sizeof(struct ast_fbdev), GFP_KERNEL);
  260. if (!afbdev)
  261. return -ENOMEM;
  262. ast->fbdev = afbdev;
  263. spin_lock_init(&afbdev->dirty_lock);
  264. drm_fb_helper_prepare(dev, &afbdev->helper, &ast_fb_helper_funcs);
  265. ret = drm_fb_helper_init(dev, &afbdev->helper, 1);
  266. if (ret)
  267. goto free;
  268. ret = drm_fb_helper_single_add_all_connectors(&afbdev->helper);
  269. if (ret)
  270. goto fini;
  271. /* disable all the possible outputs/crtcs before entering KMS mode */
  272. drm_helper_disable_unused_functions(dev);
  273. ret = drm_fb_helper_initial_config(&afbdev->helper, 32);
  274. if (ret)
  275. goto fini;
  276. return 0;
  277. fini:
  278. drm_fb_helper_fini(&afbdev->helper);
  279. free:
  280. kfree(afbdev);
  281. return ret;
  282. }
  283. void ast_fbdev_fini(struct drm_device *dev)
  284. {
  285. struct ast_private *ast = dev->dev_private;
  286. if (!ast->fbdev)
  287. return;
  288. ast_fbdev_destroy(dev, ast->fbdev);
  289. kfree(ast->fbdev);
  290. ast->fbdev = NULL;
  291. }
  292. void ast_fbdev_set_suspend(struct drm_device *dev, int state)
  293. {
  294. struct ast_private *ast = dev->dev_private;
  295. if (!ast->fbdev)
  296. return;
  297. drm_fb_helper_set_suspend(&ast->fbdev->helper, state);
  298. }
  299. void ast_fbdev_set_base(struct ast_private *ast, unsigned long gpu_addr)
  300. {
  301. ast->fbdev->helper.fbdev->fix.smem_start =
  302. ast->fbdev->helper.fbdev->apertures->ranges[0].base + gpu_addr;
  303. ast->fbdev->helper.fbdev->fix.smem_len = ast->vram_size - gpu_addr;
  304. }