radeon_fb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * Copyright © 2007 David Airlie
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * David Airlie
  25. */
  26. #include <linux/module.h>
  27. #include <linux/slab.h>
  28. #include <linux/fb.h>
  29. #include <drm/drmP.h>
  30. #include <drm/drm_crtc.h>
  31. #include <drm/drm_crtc_helper.h>
  32. #include <drm/radeon_drm.h>
  33. #include "radeon.h"
  34. #include <drm/drm_fb_helper.h>
  35. #include <linux/vga_switcheroo.h>
  36. /* object hierarchy -
  37. this contains a helper + a radeon fb
  38. the helper contains a pointer to radeon framebuffer baseclass.
  39. */
  40. struct radeon_fbdev {
  41. struct drm_fb_helper helper;
  42. struct radeon_framebuffer rfb;
  43. struct list_head fbdev_list;
  44. struct radeon_device *rdev;
  45. };
  46. static struct fb_ops radeonfb_ops = {
  47. .owner = THIS_MODULE,
  48. .fb_check_var = drm_fb_helper_check_var,
  49. .fb_set_par = drm_fb_helper_set_par,
  50. .fb_fillrect = drm_fb_helper_cfb_fillrect,
  51. .fb_copyarea = drm_fb_helper_cfb_copyarea,
  52. .fb_imageblit = drm_fb_helper_cfb_imageblit,
  53. .fb_pan_display = drm_fb_helper_pan_display,
  54. .fb_blank = drm_fb_helper_blank,
  55. .fb_setcmap = drm_fb_helper_setcmap,
  56. .fb_debug_enter = drm_fb_helper_debug_enter,
  57. .fb_debug_leave = drm_fb_helper_debug_leave,
  58. };
  59. int radeon_align_pitch(struct radeon_device *rdev, int width, int bpp, bool tiled)
  60. {
  61. int aligned = width;
  62. int align_large = (ASIC_IS_AVIVO(rdev)) || tiled;
  63. int pitch_mask = 0;
  64. switch (bpp / 8) {
  65. case 1:
  66. pitch_mask = align_large ? 255 : 127;
  67. break;
  68. case 2:
  69. pitch_mask = align_large ? 127 : 31;
  70. break;
  71. case 3:
  72. case 4:
  73. pitch_mask = align_large ? 63 : 15;
  74. break;
  75. }
  76. aligned += pitch_mask;
  77. aligned &= ~pitch_mask;
  78. return aligned;
  79. }
  80. static void radeonfb_destroy_pinned_object(struct drm_gem_object *gobj)
  81. {
  82. struct radeon_bo *rbo = gem_to_radeon_bo(gobj);
  83. int ret;
  84. ret = radeon_bo_reserve(rbo, false);
  85. if (likely(ret == 0)) {
  86. radeon_bo_kunmap(rbo);
  87. radeon_bo_unpin(rbo);
  88. radeon_bo_unreserve(rbo);
  89. }
  90. drm_gem_object_unreference_unlocked(gobj);
  91. }
  92. static int radeonfb_create_pinned_object(struct radeon_fbdev *rfbdev,
  93. struct drm_mode_fb_cmd2 *mode_cmd,
  94. struct drm_gem_object **gobj_p)
  95. {
  96. struct radeon_device *rdev = rfbdev->rdev;
  97. struct drm_gem_object *gobj = NULL;
  98. struct radeon_bo *rbo = NULL;
  99. bool fb_tiled = false; /* useful for testing */
  100. u32 tiling_flags = 0;
  101. int ret;
  102. int aligned_size, size;
  103. int height = mode_cmd->height;
  104. u32 bpp, depth;
  105. drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
  106. /* need to align pitch with crtc limits */
  107. mode_cmd->pitches[0] = radeon_align_pitch(rdev, mode_cmd->width, bpp,
  108. fb_tiled) * ((bpp + 1) / 8);
  109. if (rdev->family >= CHIP_R600)
  110. height = ALIGN(mode_cmd->height, 8);
  111. size = mode_cmd->pitches[0] * height;
  112. aligned_size = ALIGN(size, PAGE_SIZE);
  113. ret = radeon_gem_object_create(rdev, aligned_size, 0,
  114. RADEON_GEM_DOMAIN_VRAM,
  115. 0, true, &gobj);
  116. if (ret) {
  117. printk(KERN_ERR "failed to allocate framebuffer (%d)\n",
  118. aligned_size);
  119. return -ENOMEM;
  120. }
  121. rbo = gem_to_radeon_bo(gobj);
  122. if (fb_tiled)
  123. tiling_flags = RADEON_TILING_MACRO;
  124. #ifdef __BIG_ENDIAN
  125. switch (bpp) {
  126. case 32:
  127. tiling_flags |= RADEON_TILING_SWAP_32BIT;
  128. break;
  129. case 16:
  130. tiling_flags |= RADEON_TILING_SWAP_16BIT;
  131. default:
  132. break;
  133. }
  134. #endif
  135. if (tiling_flags) {
  136. ret = radeon_bo_set_tiling_flags(rbo,
  137. tiling_flags | RADEON_TILING_SURFACE,
  138. mode_cmd->pitches[0]);
  139. if (ret)
  140. dev_err(rdev->dev, "FB failed to set tiling flags\n");
  141. }
  142. ret = radeon_bo_reserve(rbo, false);
  143. if (unlikely(ret != 0))
  144. goto out_unref;
  145. /* Only 27 bit offset for legacy CRTC */
  146. ret = radeon_bo_pin_restricted(rbo, RADEON_GEM_DOMAIN_VRAM,
  147. ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27,
  148. NULL);
  149. if (ret) {
  150. radeon_bo_unreserve(rbo);
  151. goto out_unref;
  152. }
  153. if (fb_tiled)
  154. radeon_bo_check_tiling(rbo, 0, 0);
  155. ret = radeon_bo_kmap(rbo, NULL);
  156. radeon_bo_unreserve(rbo);
  157. if (ret) {
  158. goto out_unref;
  159. }
  160. *gobj_p = gobj;
  161. return 0;
  162. out_unref:
  163. radeonfb_destroy_pinned_object(gobj);
  164. *gobj_p = NULL;
  165. return ret;
  166. }
  167. static int radeonfb_create(struct drm_fb_helper *helper,
  168. struct drm_fb_helper_surface_size *sizes)
  169. {
  170. struct radeon_fbdev *rfbdev =
  171. container_of(helper, struct radeon_fbdev, helper);
  172. struct radeon_device *rdev = rfbdev->rdev;
  173. struct fb_info *info;
  174. struct drm_framebuffer *fb = NULL;
  175. struct drm_mode_fb_cmd2 mode_cmd;
  176. struct drm_gem_object *gobj = NULL;
  177. struct radeon_bo *rbo = NULL;
  178. int ret;
  179. unsigned long tmp;
  180. mode_cmd.width = sizes->surface_width;
  181. mode_cmd.height = sizes->surface_height;
  182. /* avivo can't scanout real 24bpp */
  183. if ((sizes->surface_bpp == 24) && ASIC_IS_AVIVO(rdev))
  184. sizes->surface_bpp = 32;
  185. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  186. sizes->surface_depth);
  187. ret = radeonfb_create_pinned_object(rfbdev, &mode_cmd, &gobj);
  188. if (ret) {
  189. DRM_ERROR("failed to create fbcon object %d\n", ret);
  190. return ret;
  191. }
  192. rbo = gem_to_radeon_bo(gobj);
  193. /* okay we have an object now allocate the framebuffer */
  194. info = drm_fb_helper_alloc_fbi(helper);
  195. if (IS_ERR(info)) {
  196. ret = PTR_ERR(info);
  197. goto out_unref;
  198. }
  199. info->par = rfbdev;
  200. ret = radeon_framebuffer_init(rdev->ddev, &rfbdev->rfb, &mode_cmd, gobj);
  201. if (ret) {
  202. DRM_ERROR("failed to initialize framebuffer %d\n", ret);
  203. goto out_destroy_fbi;
  204. }
  205. fb = &rfbdev->rfb.base;
  206. /* setup helper */
  207. rfbdev->helper.fb = fb;
  208. memset_io(rbo->kptr, 0x0, radeon_bo_size(rbo));
  209. strcpy(info->fix.id, "radeondrmfb");
  210. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  211. info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
  212. info->fbops = &radeonfb_ops;
  213. tmp = radeon_bo_gpu_offset(rbo) - rdev->mc.vram_start;
  214. info->fix.smem_start = rdev->mc.aper_base + tmp;
  215. info->fix.smem_len = radeon_bo_size(rbo);
  216. info->screen_base = rbo->kptr;
  217. info->screen_size = radeon_bo_size(rbo);
  218. drm_fb_helper_fill_var(info, &rfbdev->helper, sizes->fb_width, sizes->fb_height);
  219. /* setup aperture base/size for vesafb takeover */
  220. info->apertures->ranges[0].base = rdev->ddev->mode_config.fb_base;
  221. info->apertures->ranges[0].size = rdev->mc.aper_size;
  222. /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
  223. if (info->screen_base == NULL) {
  224. ret = -ENOSPC;
  225. goto out_destroy_fbi;
  226. }
  227. DRM_INFO("fb mappable at 0x%lX\n", info->fix.smem_start);
  228. DRM_INFO("vram apper at 0x%lX\n", (unsigned long)rdev->mc.aper_base);
  229. DRM_INFO("size %lu\n", (unsigned long)radeon_bo_size(rbo));
  230. DRM_INFO("fb depth is %d\n", fb->depth);
  231. DRM_INFO(" pitch is %d\n", fb->pitches[0]);
  232. vga_switcheroo_client_fb_set(rdev->ddev->pdev, info);
  233. return 0;
  234. out_destroy_fbi:
  235. drm_fb_helper_release_fbi(helper);
  236. out_unref:
  237. if (rbo) {
  238. }
  239. if (fb && ret) {
  240. drm_gem_object_unreference(gobj);
  241. drm_framebuffer_unregister_private(fb);
  242. drm_framebuffer_cleanup(fb);
  243. kfree(fb);
  244. }
  245. return ret;
  246. }
  247. void radeon_fb_output_poll_changed(struct radeon_device *rdev)
  248. {
  249. drm_fb_helper_hotplug_event(&rdev->mode_info.rfbdev->helper);
  250. }
  251. static int radeon_fbdev_destroy(struct drm_device *dev, struct radeon_fbdev *rfbdev)
  252. {
  253. struct radeon_framebuffer *rfb = &rfbdev->rfb;
  254. drm_fb_helper_unregister_fbi(&rfbdev->helper);
  255. drm_fb_helper_release_fbi(&rfbdev->helper);
  256. if (rfb->obj) {
  257. radeonfb_destroy_pinned_object(rfb->obj);
  258. rfb->obj = NULL;
  259. }
  260. drm_fb_helper_fini(&rfbdev->helper);
  261. drm_framebuffer_unregister_private(&rfb->base);
  262. drm_framebuffer_cleanup(&rfb->base);
  263. return 0;
  264. }
  265. static const struct drm_fb_helper_funcs radeon_fb_helper_funcs = {
  266. .gamma_set = radeon_crtc_fb_gamma_set,
  267. .gamma_get = radeon_crtc_fb_gamma_get,
  268. .fb_probe = radeonfb_create,
  269. };
  270. int radeon_fbdev_init(struct radeon_device *rdev)
  271. {
  272. struct radeon_fbdev *rfbdev;
  273. int bpp_sel = 32;
  274. int ret;
  275. /* select 8 bpp console on RN50 or 16MB cards */
  276. if (ASIC_IS_RN50(rdev) || rdev->mc.real_vram_size <= (32*1024*1024))
  277. bpp_sel = 8;
  278. rfbdev = kzalloc(sizeof(struct radeon_fbdev), GFP_KERNEL);
  279. if (!rfbdev)
  280. return -ENOMEM;
  281. rfbdev->rdev = rdev;
  282. rdev->mode_info.rfbdev = rfbdev;
  283. drm_fb_helper_prepare(rdev->ddev, &rfbdev->helper,
  284. &radeon_fb_helper_funcs);
  285. ret = drm_fb_helper_init(rdev->ddev, &rfbdev->helper,
  286. rdev->num_crtc,
  287. RADEONFB_CONN_LIMIT);
  288. if (ret)
  289. goto free;
  290. ret = drm_fb_helper_single_add_all_connectors(&rfbdev->helper);
  291. if (ret)
  292. goto fini;
  293. /* disable all the possible outputs/crtcs before entering KMS mode */
  294. drm_helper_disable_unused_functions(rdev->ddev);
  295. ret = drm_fb_helper_initial_config(&rfbdev->helper, bpp_sel);
  296. if (ret)
  297. goto fini;
  298. return 0;
  299. fini:
  300. drm_fb_helper_fini(&rfbdev->helper);
  301. free:
  302. kfree(rfbdev);
  303. return ret;
  304. }
  305. void radeon_fbdev_fini(struct radeon_device *rdev)
  306. {
  307. if (!rdev->mode_info.rfbdev)
  308. return;
  309. radeon_fbdev_destroy(rdev->ddev, rdev->mode_info.rfbdev);
  310. kfree(rdev->mode_info.rfbdev);
  311. rdev->mode_info.rfbdev = NULL;
  312. }
  313. void radeon_fbdev_set_suspend(struct radeon_device *rdev, int state)
  314. {
  315. fb_set_suspend(rdev->mode_info.rfbdev->helper.fbdev, state);
  316. }
  317. bool radeon_fbdev_robj_is_fb(struct radeon_device *rdev, struct radeon_bo *robj)
  318. {
  319. if (robj == gem_to_radeon_bo(rdev->mode_info.rfbdev->rfb.obj))
  320. return true;
  321. return false;
  322. }
  323. void radeon_fb_add_connector(struct radeon_device *rdev, struct drm_connector *connector)
  324. {
  325. drm_fb_helper_add_one_connector(&rdev->mode_info.rfbdev->helper, connector);
  326. }
  327. void radeon_fb_remove_connector(struct radeon_device *rdev, struct drm_connector *connector)
  328. {
  329. drm_fb_helper_remove_one_connector(&rdev->mode_info.rfbdev->helper, connector);
  330. }
  331. void radeon_fbdev_restore_mode(struct radeon_device *rdev)
  332. {
  333. struct radeon_fbdev *rfbdev = rdev->mode_info.rfbdev;
  334. struct drm_fb_helper *fb_helper;
  335. int ret;
  336. if (!rfbdev)
  337. return;
  338. fb_helper = &rfbdev->helper;
  339. ret = drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
  340. if (ret)
  341. DRM_DEBUG("failed to restore crtc mode\n");
  342. }