ast_fb.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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/fb.h>
  37. #include <linux/init.h>
  38. #include <drm/drmP.h>
  39. #include <drm/drm_crtc.h>
  40. #include <drm/drm_fb_helper.h>
  41. #include <drm/drm_crtc_helper.h>
  42. #include "ast_drv.h"
  43. static void ast_dirty_update(struct ast_fbdev *afbdev,
  44. int x, int y, int width, int height)
  45. {
  46. int i;
  47. struct drm_gem_object *obj;
  48. struct ast_bo *bo;
  49. int src_offset, dst_offset;
  50. int bpp = (afbdev->afb.base.bits_per_pixel + 7)/8;
  51. int ret;
  52. bool unmap = false;
  53. obj = afbdev->afb.obj;
  54. bo = gem_to_ast_bo(obj);
  55. ret = ast_bo_reserve(bo, true);
  56. if (ret) {
  57. DRM_ERROR("failed to reserve fb bo\n");
  58. return;
  59. }
  60. if (!bo->kmap.virtual) {
  61. ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
  62. if (ret) {
  63. DRM_ERROR("failed to kmap fb updates\n");
  64. ast_bo_unreserve(bo);
  65. return;
  66. }
  67. unmap = true;
  68. }
  69. for (i = y; i < y + height; i++) {
  70. /* assume equal stride for now */
  71. src_offset = dst_offset = i * afbdev->afb.base.pitches[0] + (x * bpp);
  72. memcpy_toio(bo->kmap.virtual + src_offset, afbdev->sysram + src_offset, width * bpp);
  73. }
  74. if (unmap)
  75. ttm_bo_kunmap(&bo->kmap);
  76. ast_bo_unreserve(bo);
  77. }
  78. static void ast_fillrect(struct fb_info *info,
  79. const struct fb_fillrect *rect)
  80. {
  81. struct ast_fbdev *afbdev = info->par;
  82. sys_fillrect(info, rect);
  83. ast_dirty_update(afbdev, rect->dx, rect->dy, rect->width,
  84. rect->height);
  85. }
  86. static void ast_copyarea(struct fb_info *info,
  87. const struct fb_copyarea *area)
  88. {
  89. struct ast_fbdev *afbdev = info->par;
  90. sys_copyarea(info, area);
  91. ast_dirty_update(afbdev, area->dx, area->dy, area->width,
  92. area->height);
  93. }
  94. static void ast_imageblit(struct fb_info *info,
  95. const struct fb_image *image)
  96. {
  97. struct ast_fbdev *afbdev = info->par;
  98. sys_imageblit(info, image);
  99. ast_dirty_update(afbdev, image->dx, image->dy, image->width,
  100. image->height);
  101. }
  102. static struct fb_ops astfb_ops = {
  103. .owner = THIS_MODULE,
  104. .fb_check_var = drm_fb_helper_check_var,
  105. .fb_set_par = drm_fb_helper_set_par,
  106. .fb_fillrect = ast_fillrect,
  107. .fb_copyarea = ast_copyarea,
  108. .fb_imageblit = ast_imageblit,
  109. .fb_pan_display = drm_fb_helper_pan_display,
  110. .fb_blank = drm_fb_helper_blank,
  111. .fb_setcmap = drm_fb_helper_setcmap,
  112. .fb_debug_enter = drm_fb_helper_debug_enter,
  113. .fb_debug_leave = drm_fb_helper_debug_leave,
  114. };
  115. static int astfb_create_object(struct ast_fbdev *afbdev,
  116. struct drm_mode_fb_cmd2 *mode_cmd,
  117. struct drm_gem_object **gobj_p)
  118. {
  119. struct drm_device *dev = afbdev->helper.dev;
  120. u32 bpp, depth;
  121. u32 size;
  122. struct drm_gem_object *gobj;
  123. int ret = 0;
  124. drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
  125. size = mode_cmd->pitches[0] * mode_cmd->height;
  126. ret = ast_gem_create(dev, size, true, &gobj);
  127. if (ret)
  128. return ret;
  129. *gobj_p = gobj;
  130. return ret;
  131. }
  132. static int astfb_create(struct drm_fb_helper *helper,
  133. struct drm_fb_helper_surface_size *sizes)
  134. {
  135. struct ast_fbdev *afbdev = (struct ast_fbdev *)helper;
  136. struct drm_device *dev = afbdev->helper.dev;
  137. struct drm_mode_fb_cmd2 mode_cmd;
  138. struct drm_framebuffer *fb;
  139. struct fb_info *info;
  140. int size, ret;
  141. struct device *device = &dev->pdev->dev;
  142. void *sysram;
  143. struct drm_gem_object *gobj = NULL;
  144. struct ast_bo *bo = NULL;
  145. mode_cmd.width = sizes->surface_width;
  146. mode_cmd.height = sizes->surface_height;
  147. mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7)/8);
  148. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  149. sizes->surface_depth);
  150. size = mode_cmd.pitches[0] * mode_cmd.height;
  151. ret = astfb_create_object(afbdev, &mode_cmd, &gobj);
  152. if (ret) {
  153. DRM_ERROR("failed to create fbcon backing object %d\n", ret);
  154. return ret;
  155. }
  156. bo = gem_to_ast_bo(gobj);
  157. sysram = vmalloc(size);
  158. if (!sysram)
  159. return -ENOMEM;
  160. info = framebuffer_alloc(0, device);
  161. if (!info) {
  162. ret = -ENOMEM;
  163. goto out;
  164. }
  165. info->par = afbdev;
  166. ret = ast_framebuffer_init(dev, &afbdev->afb, &mode_cmd, gobj);
  167. if (ret)
  168. goto out;
  169. afbdev->sysram = sysram;
  170. afbdev->size = size;
  171. fb = &afbdev->afb.base;
  172. afbdev->helper.fb = fb;
  173. afbdev->helper.fbdev = info;
  174. strcpy(info->fix.id, "astdrmfb");
  175. info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
  176. info->fbops = &astfb_ops;
  177. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  178. if (ret) {
  179. ret = -ENOMEM;
  180. goto out;
  181. }
  182. info->apertures = alloc_apertures(1);
  183. if (!info->apertures) {
  184. ret = -ENOMEM;
  185. goto out;
  186. }
  187. info->apertures->ranges[0].base = pci_resource_start(dev->pdev, 0);
  188. info->apertures->ranges[0].size = pci_resource_len(dev->pdev, 0);
  189. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  190. drm_fb_helper_fill_var(info, &afbdev->helper, sizes->fb_width, sizes->fb_height);
  191. info->screen_base = sysram;
  192. info->screen_size = size;
  193. info->pixmap.flags = FB_PIXMAP_SYSTEM;
  194. DRM_DEBUG_KMS("allocated %dx%d\n",
  195. fb->width, fb->height);
  196. return 0;
  197. out:
  198. return ret;
  199. }
  200. static void ast_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  201. u16 blue, int regno)
  202. {
  203. struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
  204. ast_crtc->lut_r[regno] = red >> 8;
  205. ast_crtc->lut_g[regno] = green >> 8;
  206. ast_crtc->lut_b[regno] = blue >> 8;
  207. }
  208. static void ast_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  209. u16 *blue, int regno)
  210. {
  211. struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
  212. *red = ast_crtc->lut_r[regno] << 8;
  213. *green = ast_crtc->lut_g[regno] << 8;
  214. *blue = ast_crtc->lut_b[regno] << 8;
  215. }
  216. static struct drm_fb_helper_funcs ast_fb_helper_funcs = {
  217. .gamma_set = ast_fb_gamma_set,
  218. .gamma_get = ast_fb_gamma_get,
  219. .fb_probe = astfb_create,
  220. };
  221. static void ast_fbdev_destroy(struct drm_device *dev,
  222. struct ast_fbdev *afbdev)
  223. {
  224. struct fb_info *info;
  225. struct ast_framebuffer *afb = &afbdev->afb;
  226. if (afbdev->helper.fbdev) {
  227. info = afbdev->helper.fbdev;
  228. unregister_framebuffer(info);
  229. if (info->cmap.len)
  230. fb_dealloc_cmap(&info->cmap);
  231. framebuffer_release(info);
  232. }
  233. if (afb->obj) {
  234. drm_gem_object_unreference_unlocked(afb->obj);
  235. afb->obj = NULL;
  236. }
  237. drm_fb_helper_fini(&afbdev->helper);
  238. vfree(afbdev->sysram);
  239. drm_framebuffer_unregister_private(&afb->base);
  240. drm_framebuffer_cleanup(&afb->base);
  241. }
  242. int ast_fbdev_init(struct drm_device *dev)
  243. {
  244. struct ast_private *ast = dev->dev_private;
  245. struct ast_fbdev *afbdev;
  246. int ret;
  247. afbdev = kzalloc(sizeof(struct ast_fbdev), GFP_KERNEL);
  248. if (!afbdev)
  249. return -ENOMEM;
  250. ast->fbdev = afbdev;
  251. afbdev->helper.funcs = &ast_fb_helper_funcs;
  252. ret = drm_fb_helper_init(dev, &afbdev->helper,
  253. 1, 1);
  254. if (ret) {
  255. kfree(afbdev);
  256. return ret;
  257. }
  258. drm_fb_helper_single_add_all_connectors(&afbdev->helper);
  259. /* disable all the possible outputs/crtcs before entering KMS mode */
  260. drm_helper_disable_unused_functions(dev);
  261. drm_fb_helper_initial_config(&afbdev->helper, 32);
  262. return 0;
  263. }
  264. void ast_fbdev_fini(struct drm_device *dev)
  265. {
  266. struct ast_private *ast = dev->dev_private;
  267. if (!ast->fbdev)
  268. return;
  269. ast_fbdev_destroy(dev, ast->fbdev);
  270. kfree(ast->fbdev);
  271. ast->fbdev = NULL;
  272. }
  273. void ast_fbdev_set_suspend(struct drm_device *dev, int state)
  274. {
  275. struct ast_private *ast = dev->dev_private;
  276. if (!ast->fbdev)
  277. return;
  278. fb_set_suspend(ast->fbdev->helper.fbdev, state);
  279. }