radeon_fb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. /**
  47. * radeon_fb_helper_set_par - Hide cursor on CRTCs used by fbdev.
  48. *
  49. * @info: fbdev info
  50. *
  51. * This function hides the cursor on all CRTCs used by fbdev.
  52. */
  53. static int radeon_fb_helper_set_par(struct fb_info *info)
  54. {
  55. int ret;
  56. ret = drm_fb_helper_set_par(info);
  57. /* XXX: with universal plane support fbdev will automatically disable
  58. * all non-primary planes (including the cursor)
  59. */
  60. if (ret == 0) {
  61. struct drm_fb_helper *fb_helper = info->par;
  62. int i;
  63. for (i = 0; i < fb_helper->crtc_count; i++) {
  64. struct drm_crtc *crtc = fb_helper->crtc_info[i].mode_set.crtc;
  65. radeon_crtc_cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
  66. }
  67. }
  68. return ret;
  69. }
  70. static struct fb_ops radeonfb_ops = {
  71. .owner = THIS_MODULE,
  72. .fb_check_var = drm_fb_helper_check_var,
  73. .fb_set_par = radeon_fb_helper_set_par,
  74. .fb_fillrect = cfb_fillrect,
  75. .fb_copyarea = cfb_copyarea,
  76. .fb_imageblit = cfb_imageblit,
  77. .fb_pan_display = drm_fb_helper_pan_display,
  78. .fb_blank = drm_fb_helper_blank,
  79. .fb_setcmap = drm_fb_helper_setcmap,
  80. .fb_debug_enter = drm_fb_helper_debug_enter,
  81. .fb_debug_leave = drm_fb_helper_debug_leave,
  82. };
  83. int radeon_align_pitch(struct radeon_device *rdev, int width, int bpp, bool tiled)
  84. {
  85. int aligned = width;
  86. int align_large = (ASIC_IS_AVIVO(rdev)) || tiled;
  87. int pitch_mask = 0;
  88. switch (bpp / 8) {
  89. case 1:
  90. pitch_mask = align_large ? 255 : 127;
  91. break;
  92. case 2:
  93. pitch_mask = align_large ? 127 : 31;
  94. break;
  95. case 3:
  96. case 4:
  97. pitch_mask = align_large ? 63 : 15;
  98. break;
  99. }
  100. aligned += pitch_mask;
  101. aligned &= ~pitch_mask;
  102. return aligned;
  103. }
  104. static void radeonfb_destroy_pinned_object(struct drm_gem_object *gobj)
  105. {
  106. struct radeon_bo *rbo = gem_to_radeon_bo(gobj);
  107. int ret;
  108. ret = radeon_bo_reserve(rbo, false);
  109. if (likely(ret == 0)) {
  110. radeon_bo_kunmap(rbo);
  111. radeon_bo_unpin(rbo);
  112. radeon_bo_unreserve(rbo);
  113. }
  114. drm_gem_object_unreference_unlocked(gobj);
  115. }
  116. static int radeonfb_create_pinned_object(struct radeon_fbdev *rfbdev,
  117. struct drm_mode_fb_cmd2 *mode_cmd,
  118. struct drm_gem_object **gobj_p)
  119. {
  120. struct radeon_device *rdev = rfbdev->rdev;
  121. struct drm_gem_object *gobj = NULL;
  122. struct radeon_bo *rbo = NULL;
  123. bool fb_tiled = false; /* useful for testing */
  124. u32 tiling_flags = 0;
  125. int ret;
  126. int aligned_size, size;
  127. int height = mode_cmd->height;
  128. u32 bpp, depth;
  129. drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
  130. /* need to align pitch with crtc limits */
  131. mode_cmd->pitches[0] = radeon_align_pitch(rdev, mode_cmd->width, bpp,
  132. fb_tiled) * ((bpp + 1) / 8);
  133. if (rdev->family >= CHIP_R600)
  134. height = ALIGN(mode_cmd->height, 8);
  135. size = mode_cmd->pitches[0] * height;
  136. aligned_size = ALIGN(size, PAGE_SIZE);
  137. ret = radeon_gem_object_create(rdev, aligned_size, 0,
  138. RADEON_GEM_DOMAIN_VRAM,
  139. 0, true, &gobj);
  140. if (ret) {
  141. printk(KERN_ERR "failed to allocate framebuffer (%d)\n",
  142. aligned_size);
  143. return -ENOMEM;
  144. }
  145. rbo = gem_to_radeon_bo(gobj);
  146. if (fb_tiled)
  147. tiling_flags = RADEON_TILING_MACRO;
  148. #ifdef __BIG_ENDIAN
  149. switch (bpp) {
  150. case 32:
  151. tiling_flags |= RADEON_TILING_SWAP_32BIT;
  152. break;
  153. case 16:
  154. tiling_flags |= RADEON_TILING_SWAP_16BIT;
  155. default:
  156. break;
  157. }
  158. #endif
  159. if (tiling_flags) {
  160. ret = radeon_bo_set_tiling_flags(rbo,
  161. tiling_flags | RADEON_TILING_SURFACE,
  162. mode_cmd->pitches[0]);
  163. if (ret)
  164. dev_err(rdev->dev, "FB failed to set tiling flags\n");
  165. }
  166. ret = radeon_bo_reserve(rbo, false);
  167. if (unlikely(ret != 0))
  168. goto out_unref;
  169. /* Only 27 bit offset for legacy CRTC */
  170. ret = radeon_bo_pin_restricted(rbo, RADEON_GEM_DOMAIN_VRAM,
  171. ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27,
  172. NULL);
  173. if (ret) {
  174. radeon_bo_unreserve(rbo);
  175. goto out_unref;
  176. }
  177. if (fb_tiled)
  178. radeon_bo_check_tiling(rbo, 0, 0);
  179. ret = radeon_bo_kmap(rbo, NULL);
  180. radeon_bo_unreserve(rbo);
  181. if (ret) {
  182. goto out_unref;
  183. }
  184. *gobj_p = gobj;
  185. return 0;
  186. out_unref:
  187. radeonfb_destroy_pinned_object(gobj);
  188. *gobj_p = NULL;
  189. return ret;
  190. }
  191. static int radeonfb_create(struct drm_fb_helper *helper,
  192. struct drm_fb_helper_surface_size *sizes)
  193. {
  194. struct radeon_fbdev *rfbdev =
  195. container_of(helper, struct radeon_fbdev, helper);
  196. struct radeon_device *rdev = rfbdev->rdev;
  197. struct fb_info *info;
  198. struct drm_framebuffer *fb = NULL;
  199. struct drm_mode_fb_cmd2 mode_cmd;
  200. struct drm_gem_object *gobj = NULL;
  201. struct radeon_bo *rbo = NULL;
  202. struct device *device = &rdev->pdev->dev;
  203. int ret;
  204. unsigned long tmp;
  205. mode_cmd.width = sizes->surface_width;
  206. mode_cmd.height = sizes->surface_height;
  207. /* avivo can't scanout real 24bpp */
  208. if ((sizes->surface_bpp == 24) && ASIC_IS_AVIVO(rdev))
  209. sizes->surface_bpp = 32;
  210. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  211. sizes->surface_depth);
  212. ret = radeonfb_create_pinned_object(rfbdev, &mode_cmd, &gobj);
  213. if (ret) {
  214. DRM_ERROR("failed to create fbcon object %d\n", ret);
  215. return ret;
  216. }
  217. rbo = gem_to_radeon_bo(gobj);
  218. /* okay we have an object now allocate the framebuffer */
  219. info = framebuffer_alloc(0, device);
  220. if (info == NULL) {
  221. ret = -ENOMEM;
  222. goto out_unref;
  223. }
  224. info->par = rfbdev;
  225. ret = radeon_framebuffer_init(rdev->ddev, &rfbdev->rfb, &mode_cmd, gobj);
  226. if (ret) {
  227. DRM_ERROR("failed to initialize framebuffer %d\n", ret);
  228. goto out_unref;
  229. }
  230. fb = &rfbdev->rfb.base;
  231. /* setup helper */
  232. rfbdev->helper.fb = fb;
  233. rfbdev->helper.fbdev = info;
  234. memset_io(rbo->kptr, 0x0, radeon_bo_size(rbo));
  235. strcpy(info->fix.id, "radeondrmfb");
  236. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  237. info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
  238. info->fbops = &radeonfb_ops;
  239. tmp = radeon_bo_gpu_offset(rbo) - rdev->mc.vram_start;
  240. info->fix.smem_start = rdev->mc.aper_base + tmp;
  241. info->fix.smem_len = radeon_bo_size(rbo);
  242. info->screen_base = rbo->kptr;
  243. info->screen_size = radeon_bo_size(rbo);
  244. drm_fb_helper_fill_var(info, &rfbdev->helper, sizes->fb_width, sizes->fb_height);
  245. /* setup aperture base/size for vesafb takeover */
  246. info->apertures = alloc_apertures(1);
  247. if (!info->apertures) {
  248. ret = -ENOMEM;
  249. goto out_unref;
  250. }
  251. info->apertures->ranges[0].base = rdev->ddev->mode_config.fb_base;
  252. info->apertures->ranges[0].size = rdev->mc.aper_size;
  253. /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
  254. if (info->screen_base == NULL) {
  255. ret = -ENOSPC;
  256. goto out_unref;
  257. }
  258. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  259. if (ret) {
  260. ret = -ENOMEM;
  261. goto out_unref;
  262. }
  263. DRM_INFO("fb mappable at 0x%lX\n", info->fix.smem_start);
  264. DRM_INFO("vram apper at 0x%lX\n", (unsigned long)rdev->mc.aper_base);
  265. DRM_INFO("size %lu\n", (unsigned long)radeon_bo_size(rbo));
  266. DRM_INFO("fb depth is %d\n", fb->depth);
  267. DRM_INFO(" pitch is %d\n", fb->pitches[0]);
  268. vga_switcheroo_client_fb_set(rdev->ddev->pdev, info);
  269. return 0;
  270. out_unref:
  271. if (rbo) {
  272. }
  273. if (fb && ret) {
  274. drm_gem_object_unreference(gobj);
  275. drm_framebuffer_unregister_private(fb);
  276. drm_framebuffer_cleanup(fb);
  277. kfree(fb);
  278. }
  279. return ret;
  280. }
  281. void radeon_fb_output_poll_changed(struct radeon_device *rdev)
  282. {
  283. drm_fb_helper_hotplug_event(&rdev->mode_info.rfbdev->helper);
  284. }
  285. static int radeon_fbdev_destroy(struct drm_device *dev, struct radeon_fbdev *rfbdev)
  286. {
  287. struct fb_info *info;
  288. struct radeon_framebuffer *rfb = &rfbdev->rfb;
  289. if (rfbdev->helper.fbdev) {
  290. info = rfbdev->helper.fbdev;
  291. unregister_framebuffer(info);
  292. if (info->cmap.len)
  293. fb_dealloc_cmap(&info->cmap);
  294. framebuffer_release(info);
  295. }
  296. if (rfb->obj) {
  297. radeonfb_destroy_pinned_object(rfb->obj);
  298. rfb->obj = NULL;
  299. }
  300. drm_fb_helper_fini(&rfbdev->helper);
  301. drm_framebuffer_unregister_private(&rfb->base);
  302. drm_framebuffer_cleanup(&rfb->base);
  303. return 0;
  304. }
  305. static const struct drm_fb_helper_funcs radeon_fb_helper_funcs = {
  306. .gamma_set = radeon_crtc_fb_gamma_set,
  307. .gamma_get = radeon_crtc_fb_gamma_get,
  308. .fb_probe = radeonfb_create,
  309. };
  310. int radeon_fbdev_init(struct radeon_device *rdev)
  311. {
  312. struct radeon_fbdev *rfbdev;
  313. int bpp_sel = 32;
  314. int ret;
  315. /* select 8 bpp console on RN50 or 16MB cards */
  316. if (ASIC_IS_RN50(rdev) || rdev->mc.real_vram_size <= (32*1024*1024))
  317. bpp_sel = 8;
  318. rfbdev = kzalloc(sizeof(struct radeon_fbdev), GFP_KERNEL);
  319. if (!rfbdev)
  320. return -ENOMEM;
  321. rfbdev->rdev = rdev;
  322. rdev->mode_info.rfbdev = rfbdev;
  323. drm_fb_helper_prepare(rdev->ddev, &rfbdev->helper,
  324. &radeon_fb_helper_funcs);
  325. ret = drm_fb_helper_init(rdev->ddev, &rfbdev->helper,
  326. rdev->num_crtc,
  327. RADEONFB_CONN_LIMIT);
  328. if (ret) {
  329. kfree(rfbdev);
  330. return ret;
  331. }
  332. drm_fb_helper_single_add_all_connectors(&rfbdev->helper);
  333. /* disable all the possible outputs/crtcs before entering KMS mode */
  334. drm_helper_disable_unused_functions(rdev->ddev);
  335. drm_fb_helper_initial_config(&rfbdev->helper, bpp_sel);
  336. return 0;
  337. }
  338. void radeon_fbdev_fini(struct radeon_device *rdev)
  339. {
  340. if (!rdev->mode_info.rfbdev)
  341. return;
  342. radeon_fbdev_destroy(rdev->ddev, rdev->mode_info.rfbdev);
  343. kfree(rdev->mode_info.rfbdev);
  344. rdev->mode_info.rfbdev = NULL;
  345. }
  346. void radeon_fbdev_set_suspend(struct radeon_device *rdev, int state)
  347. {
  348. fb_set_suspend(rdev->mode_info.rfbdev->helper.fbdev, state);
  349. }
  350. int radeon_fbdev_total_size(struct radeon_device *rdev)
  351. {
  352. struct radeon_bo *robj;
  353. int size = 0;
  354. robj = gem_to_radeon_bo(rdev->mode_info.rfbdev->rfb.obj);
  355. size += radeon_bo_size(robj);
  356. return size;
  357. }
  358. bool radeon_fbdev_robj_is_fb(struct radeon_device *rdev, struct radeon_bo *robj)
  359. {
  360. if (robj == gem_to_radeon_bo(rdev->mode_info.rfbdev->rfb.obj))
  361. return true;
  362. return false;
  363. }