ast_fb.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 = -EBUSY;
  52. bool unmap = false;
  53. bool store_for_later = false;
  54. int x2, y2;
  55. unsigned long flags;
  56. obj = afbdev->afb.obj;
  57. bo = gem_to_ast_bo(obj);
  58. /*
  59. * try and reserve the BO, if we fail with busy
  60. * then the BO is being moved and we should
  61. * store up the damage until later.
  62. */
  63. if (drm_can_sleep())
  64. ret = ast_bo_reserve(bo, true);
  65. if (ret) {
  66. if (ret != -EBUSY)
  67. return;
  68. store_for_later = true;
  69. }
  70. x2 = x + width - 1;
  71. y2 = y + height - 1;
  72. spin_lock_irqsave(&afbdev->dirty_lock, flags);
  73. if (afbdev->y1 < y)
  74. y = afbdev->y1;
  75. if (afbdev->y2 > y2)
  76. y2 = afbdev->y2;
  77. if (afbdev->x1 < x)
  78. x = afbdev->x1;
  79. if (afbdev->x2 > x2)
  80. x2 = afbdev->x2;
  81. if (store_for_later) {
  82. afbdev->x1 = x;
  83. afbdev->x2 = x2;
  84. afbdev->y1 = y;
  85. afbdev->y2 = y2;
  86. spin_unlock_irqrestore(&afbdev->dirty_lock, flags);
  87. return;
  88. }
  89. afbdev->x1 = afbdev->y1 = INT_MAX;
  90. afbdev->x2 = afbdev->y2 = 0;
  91. spin_unlock_irqrestore(&afbdev->dirty_lock, flags);
  92. if (!bo->kmap.virtual) {
  93. ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
  94. if (ret) {
  95. DRM_ERROR("failed to kmap fb updates\n");
  96. ast_bo_unreserve(bo);
  97. return;
  98. }
  99. unmap = true;
  100. }
  101. for (i = y; i <= y2; i++) {
  102. /* assume equal stride for now */
  103. src_offset = dst_offset = i * afbdev->afb.base.pitches[0] + (x * bpp);
  104. memcpy_toio(bo->kmap.virtual + src_offset, afbdev->sysram + src_offset, (x2 - x + 1) * bpp);
  105. }
  106. if (unmap)
  107. ttm_bo_kunmap(&bo->kmap);
  108. ast_bo_unreserve(bo);
  109. }
  110. static void ast_fillrect(struct fb_info *info,
  111. const struct fb_fillrect *rect)
  112. {
  113. struct ast_fbdev *afbdev = info->par;
  114. drm_fb_helper_sys_fillrect(info, rect);
  115. ast_dirty_update(afbdev, rect->dx, rect->dy, rect->width,
  116. rect->height);
  117. }
  118. static void ast_copyarea(struct fb_info *info,
  119. const struct fb_copyarea *area)
  120. {
  121. struct ast_fbdev *afbdev = info->par;
  122. drm_fb_helper_sys_copyarea(info, area);
  123. ast_dirty_update(afbdev, area->dx, area->dy, area->width,
  124. area->height);
  125. }
  126. static void ast_imageblit(struct fb_info *info,
  127. const struct fb_image *image)
  128. {
  129. struct ast_fbdev *afbdev = info->par;
  130. drm_fb_helper_sys_imageblit(info, image);
  131. ast_dirty_update(afbdev, image->dx, image->dy, image->width,
  132. image->height);
  133. }
  134. static struct fb_ops astfb_ops = {
  135. .owner = THIS_MODULE,
  136. .fb_check_var = drm_fb_helper_check_var,
  137. .fb_set_par = drm_fb_helper_set_par,
  138. .fb_fillrect = ast_fillrect,
  139. .fb_copyarea = ast_copyarea,
  140. .fb_imageblit = ast_imageblit,
  141. .fb_pan_display = drm_fb_helper_pan_display,
  142. .fb_blank = drm_fb_helper_blank,
  143. .fb_setcmap = drm_fb_helper_setcmap,
  144. .fb_debug_enter = drm_fb_helper_debug_enter,
  145. .fb_debug_leave = drm_fb_helper_debug_leave,
  146. };
  147. static int astfb_create_object(struct ast_fbdev *afbdev,
  148. struct drm_mode_fb_cmd2 *mode_cmd,
  149. struct drm_gem_object **gobj_p)
  150. {
  151. struct drm_device *dev = afbdev->helper.dev;
  152. u32 bpp, depth;
  153. u32 size;
  154. struct drm_gem_object *gobj;
  155. int ret = 0;
  156. drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
  157. size = mode_cmd->pitches[0] * mode_cmd->height;
  158. ret = ast_gem_create(dev, size, true, &gobj);
  159. if (ret)
  160. return ret;
  161. *gobj_p = gobj;
  162. return ret;
  163. }
  164. static int astfb_create(struct drm_fb_helper *helper,
  165. struct drm_fb_helper_surface_size *sizes)
  166. {
  167. struct ast_fbdev *afbdev =
  168. container_of(helper, struct ast_fbdev, helper);
  169. struct drm_device *dev = afbdev->helper.dev;
  170. struct drm_mode_fb_cmd2 mode_cmd;
  171. struct drm_framebuffer *fb;
  172. struct fb_info *info;
  173. int size, ret;
  174. void *sysram;
  175. struct drm_gem_object *gobj = NULL;
  176. struct ast_bo *bo = NULL;
  177. mode_cmd.width = sizes->surface_width;
  178. mode_cmd.height = sizes->surface_height;
  179. mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7)/8);
  180. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  181. sizes->surface_depth);
  182. size = mode_cmd.pitches[0] * mode_cmd.height;
  183. ret = astfb_create_object(afbdev, &mode_cmd, &gobj);
  184. if (ret) {
  185. DRM_ERROR("failed to create fbcon backing object %d\n", ret);
  186. return ret;
  187. }
  188. bo = gem_to_ast_bo(gobj);
  189. sysram = vmalloc(size);
  190. if (!sysram)
  191. return -ENOMEM;
  192. info = drm_fb_helper_alloc_fbi(helper);
  193. if (IS_ERR(info)) {
  194. ret = PTR_ERR(info);
  195. goto err_free_vram;
  196. }
  197. info->par = afbdev;
  198. ret = ast_framebuffer_init(dev, &afbdev->afb, &mode_cmd, gobj);
  199. if (ret)
  200. goto err_release_fbi;
  201. afbdev->sysram = sysram;
  202. afbdev->size = size;
  203. fb = &afbdev->afb.base;
  204. afbdev->helper.fb = fb;
  205. strcpy(info->fix.id, "astdrmfb");
  206. info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
  207. info->fbops = &astfb_ops;
  208. info->apertures->ranges[0].base = pci_resource_start(dev->pdev, 0);
  209. info->apertures->ranges[0].size = pci_resource_len(dev->pdev, 0);
  210. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  211. drm_fb_helper_fill_var(info, &afbdev->helper, sizes->fb_width, sizes->fb_height);
  212. info->screen_base = sysram;
  213. info->screen_size = size;
  214. info->pixmap.flags = FB_PIXMAP_SYSTEM;
  215. DRM_DEBUG_KMS("allocated %dx%d\n",
  216. fb->width, fb->height);
  217. return 0;
  218. err_release_fbi:
  219. drm_fb_helper_release_fbi(helper);
  220. err_free_vram:
  221. vfree(afbdev->sysram);
  222. return ret;
  223. }
  224. static void ast_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  225. u16 blue, int regno)
  226. {
  227. struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
  228. ast_crtc->lut_r[regno] = red >> 8;
  229. ast_crtc->lut_g[regno] = green >> 8;
  230. ast_crtc->lut_b[regno] = blue >> 8;
  231. }
  232. static void ast_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  233. u16 *blue, int regno)
  234. {
  235. struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
  236. *red = ast_crtc->lut_r[regno] << 8;
  237. *green = ast_crtc->lut_g[regno] << 8;
  238. *blue = ast_crtc->lut_b[regno] << 8;
  239. }
  240. static const struct drm_fb_helper_funcs ast_fb_helper_funcs = {
  241. .gamma_set = ast_fb_gamma_set,
  242. .gamma_get = ast_fb_gamma_get,
  243. .fb_probe = astfb_create,
  244. };
  245. static void ast_fbdev_destroy(struct drm_device *dev,
  246. struct ast_fbdev *afbdev)
  247. {
  248. struct ast_framebuffer *afb = &afbdev->afb;
  249. drm_fb_helper_unregister_fbi(&afbdev->helper);
  250. drm_fb_helper_release_fbi(&afbdev->helper);
  251. if (afb->obj) {
  252. drm_gem_object_unreference_unlocked(afb->obj);
  253. afb->obj = NULL;
  254. }
  255. drm_fb_helper_fini(&afbdev->helper);
  256. vfree(afbdev->sysram);
  257. drm_framebuffer_unregister_private(&afb->base);
  258. drm_framebuffer_cleanup(&afb->base);
  259. }
  260. int ast_fbdev_init(struct drm_device *dev)
  261. {
  262. struct ast_private *ast = dev->dev_private;
  263. struct ast_fbdev *afbdev;
  264. int ret;
  265. afbdev = kzalloc(sizeof(struct ast_fbdev), GFP_KERNEL);
  266. if (!afbdev)
  267. return -ENOMEM;
  268. ast->fbdev = afbdev;
  269. spin_lock_init(&afbdev->dirty_lock);
  270. drm_fb_helper_prepare(dev, &afbdev->helper, &ast_fb_helper_funcs);
  271. ret = drm_fb_helper_init(dev, &afbdev->helper,
  272. 1, 1);
  273. if (ret)
  274. goto free;
  275. ret = drm_fb_helper_single_add_all_connectors(&afbdev->helper);
  276. if (ret)
  277. goto fini;
  278. /* disable all the possible outputs/crtcs before entering KMS mode */
  279. drm_helper_disable_unused_functions(dev);
  280. ret = drm_fb_helper_initial_config(&afbdev->helper, 32);
  281. if (ret)
  282. goto fini;
  283. return 0;
  284. fini:
  285. drm_fb_helper_fini(&afbdev->helper);
  286. free:
  287. kfree(afbdev);
  288. return ret;
  289. }
  290. void ast_fbdev_fini(struct drm_device *dev)
  291. {
  292. struct ast_private *ast = dev->dev_private;
  293. if (!ast->fbdev)
  294. return;
  295. ast_fbdev_destroy(dev, ast->fbdev);
  296. kfree(ast->fbdev);
  297. ast->fbdev = NULL;
  298. }
  299. void ast_fbdev_set_suspend(struct drm_device *dev, int state)
  300. {
  301. struct ast_private *ast = dev->dev_private;
  302. if (!ast->fbdev)
  303. return;
  304. drm_fb_helper_set_suspend(&ast->fbdev->helper, state);
  305. }
  306. void ast_fbdev_set_base(struct ast_private *ast, unsigned long gpu_addr)
  307. {
  308. ast->fbdev->helper.fbdev->fix.smem_start =
  309. ast->fbdev->helper.fbdev->apertures->ranges[0].base + gpu_addr;
  310. ast->fbdev->helper.fbdev->fix.smem_len = ast->vram_size - gpu_addr;
  311. }