ast_fb.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 "drmP.h"
  39. #include "drm.h"
  40. #include "drm_crtc.h"
  41. #include "drm_fb_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 ast_fbdev *afbdev,
  133. struct drm_fb_helper_surface_size *sizes)
  134. {
  135. struct drm_device *dev = afbdev->helper.dev;
  136. struct drm_mode_fb_cmd2 mode_cmd;
  137. struct drm_framebuffer *fb;
  138. struct fb_info *info;
  139. int size, ret;
  140. struct device *device = &dev->pdev->dev;
  141. void *sysram;
  142. struct drm_gem_object *gobj = NULL;
  143. struct ast_bo *bo = NULL;
  144. mode_cmd.width = sizes->surface_width;
  145. mode_cmd.height = sizes->surface_height;
  146. mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7)/8);
  147. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  148. sizes->surface_depth);
  149. size = mode_cmd.pitches[0] * mode_cmd.height;
  150. ret = astfb_create_object(afbdev, &mode_cmd, &gobj);
  151. if (ret) {
  152. DRM_ERROR("failed to create fbcon backing object %d\n", ret);
  153. return ret;
  154. }
  155. bo = gem_to_ast_bo(gobj);
  156. sysram = vmalloc(size);
  157. if (!sysram)
  158. return -ENOMEM;
  159. info = framebuffer_alloc(0, device);
  160. if (!info) {
  161. ret = -ENOMEM;
  162. goto out;
  163. }
  164. info->par = afbdev;
  165. ret = ast_framebuffer_init(dev, &afbdev->afb, &mode_cmd, gobj);
  166. if (ret)
  167. goto out;
  168. afbdev->sysram = sysram;
  169. afbdev->size = size;
  170. fb = &afbdev->afb.base;
  171. afbdev->helper.fb = fb;
  172. afbdev->helper.fbdev = info;
  173. strcpy(info->fix.id, "astdrmfb");
  174. info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
  175. info->fbops = &astfb_ops;
  176. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  177. if (ret) {
  178. ret = -ENOMEM;
  179. goto out;
  180. }
  181. info->apertures = alloc_apertures(1);
  182. if (!info->apertures) {
  183. ret = -ENOMEM;
  184. goto out;
  185. }
  186. info->apertures->ranges[0].base = pci_resource_start(dev->pdev, 0);
  187. info->apertures->ranges[0].size = pci_resource_len(dev->pdev, 0);
  188. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  189. drm_fb_helper_fill_var(info, &afbdev->helper, sizes->fb_width, sizes->fb_height);
  190. info->screen_base = sysram;
  191. info->screen_size = size;
  192. info->pixmap.flags = FB_PIXMAP_SYSTEM;
  193. DRM_DEBUG_KMS("allocated %dx%d\n",
  194. fb->width, fb->height);
  195. return 0;
  196. out:
  197. return ret;
  198. }
  199. static void ast_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  200. u16 blue, int regno)
  201. {
  202. struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
  203. ast_crtc->lut_r[regno] = red >> 8;
  204. ast_crtc->lut_g[regno] = green >> 8;
  205. ast_crtc->lut_b[regno] = blue >> 8;
  206. }
  207. static void ast_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  208. u16 *blue, int regno)
  209. {
  210. struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
  211. *red = ast_crtc->lut_r[regno] << 8;
  212. *green = ast_crtc->lut_g[regno] << 8;
  213. *blue = ast_crtc->lut_b[regno] << 8;
  214. }
  215. static int ast_find_or_create_single(struct drm_fb_helper *helper,
  216. struct drm_fb_helper_surface_size *sizes)
  217. {
  218. struct ast_fbdev *afbdev = (struct ast_fbdev *)helper;
  219. int new_fb = 0;
  220. int ret;
  221. if (!helper->fb) {
  222. ret = astfb_create(afbdev, sizes);
  223. if (ret)
  224. return ret;
  225. new_fb = 1;
  226. }
  227. return new_fb;
  228. }
  229. static struct drm_fb_helper_funcs ast_fb_helper_funcs = {
  230. .gamma_set = ast_fb_gamma_set,
  231. .gamma_get = ast_fb_gamma_get,
  232. .fb_probe = ast_find_or_create_single,
  233. };
  234. static void ast_fbdev_destroy(struct drm_device *dev,
  235. struct ast_fbdev *afbdev)
  236. {
  237. struct fb_info *info;
  238. struct ast_framebuffer *afb = &afbdev->afb;
  239. if (afbdev->helper.fbdev) {
  240. info = afbdev->helper.fbdev;
  241. unregister_framebuffer(info);
  242. if (info->cmap.len)
  243. fb_dealloc_cmap(&info->cmap);
  244. framebuffer_release(info);
  245. }
  246. if (afb->obj) {
  247. drm_gem_object_unreference_unlocked(afb->obj);
  248. afb->obj = NULL;
  249. }
  250. drm_fb_helper_fini(&afbdev->helper);
  251. vfree(afbdev->sysram);
  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. afbdev->helper.funcs = &ast_fb_helper_funcs;
  264. ret = drm_fb_helper_init(dev, &afbdev->helper,
  265. 1, 1);
  266. if (ret) {
  267. kfree(afbdev);
  268. return ret;
  269. }
  270. drm_fb_helper_single_add_all_connectors(&afbdev->helper);
  271. drm_fb_helper_initial_config(&afbdev->helper, 32);
  272. return 0;
  273. }
  274. void ast_fbdev_fini(struct drm_device *dev)
  275. {
  276. struct ast_private *ast = dev->dev_private;
  277. if (!ast->fbdev)
  278. return;
  279. ast_fbdev_destroy(dev, ast->fbdev);
  280. kfree(ast->fbdev);
  281. ast->fbdev = NULL;
  282. }
  283. void ast_fbdev_set_suspend(struct drm_device *dev, int state)
  284. {
  285. struct ast_private *ast = dev->dev_private;
  286. if (!ast->fbdev)
  287. return;
  288. fb_set_suspend(ast->fbdev->helper.fbdev, state);
  289. }