radeon_fb.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 "drmP.h"
  30. #include "drm.h"
  31. #include "drm_crtc.h"
  32. #include "drm_crtc_helper.h"
  33. #include "radeon_drm.h"
  34. #include "radeon.h"
  35. #include "drm_fb_helper.h"
  36. #include <linux/vga_switcheroo.h>
  37. /* object hierarchy -
  38. this contains a helper + a radeon fb
  39. the helper contains a pointer to radeon framebuffer baseclass.
  40. */
  41. struct radeon_fbdev {
  42. struct drm_fb_helper helper;
  43. struct radeon_framebuffer rfb;
  44. struct list_head fbdev_list;
  45. struct radeon_device *rdev;
  46. };
  47. static struct fb_ops radeonfb_ops = {
  48. .owner = THIS_MODULE,
  49. .fb_check_var = drm_fb_helper_check_var,
  50. .fb_set_par = drm_fb_helper_set_par,
  51. .fb_fillrect = cfb_fillrect,
  52. .fb_copyarea = cfb_copyarea,
  53. .fb_imageblit = cfb_imageblit,
  54. .fb_pan_display = drm_fb_helper_pan_display,
  55. .fb_blank = drm_fb_helper_blank,
  56. .fb_setcmap = drm_fb_helper_setcmap,
  57. };
  58. static int radeon_align_pitch(struct radeon_device *rdev, int width, int bpp, bool tiled)
  59. {
  60. int aligned = width;
  61. int align_large = (ASIC_IS_AVIVO(rdev)) || tiled;
  62. int pitch_mask = 0;
  63. switch (bpp / 8) {
  64. case 1:
  65. pitch_mask = align_large ? 255 : 127;
  66. break;
  67. case 2:
  68. pitch_mask = align_large ? 127 : 31;
  69. break;
  70. case 3:
  71. case 4:
  72. pitch_mask = align_large ? 63 : 15;
  73. break;
  74. }
  75. aligned += pitch_mask;
  76. aligned &= ~pitch_mask;
  77. return aligned;
  78. }
  79. static void radeonfb_destroy_pinned_object(struct drm_gem_object *gobj)
  80. {
  81. struct radeon_bo *rbo = gobj->driver_private;
  82. int ret;
  83. ret = radeon_bo_reserve(rbo, false);
  84. if (likely(ret == 0)) {
  85. radeon_bo_kunmap(rbo);
  86. radeon_bo_unpin(rbo);
  87. radeon_bo_unreserve(rbo);
  88. }
  89. drm_gem_object_handle_unreference(gobj);
  90. drm_gem_object_unreference_unlocked(gobj);
  91. }
  92. static int radeonfb_create_pinned_object(struct radeon_fbdev *rfbdev,
  93. struct drm_mode_fb_cmd *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. /* need to align pitch with crtc limits */
  104. mode_cmd->pitch = radeon_align_pitch(rdev, mode_cmd->width, mode_cmd->bpp, fb_tiled) * ((mode_cmd->bpp + 1) / 8);
  105. size = mode_cmd->pitch * mode_cmd->height;
  106. aligned_size = ALIGN(size, PAGE_SIZE);
  107. ret = radeon_gem_object_create(rdev, aligned_size, 0,
  108. RADEON_GEM_DOMAIN_VRAM,
  109. false, true,
  110. &gobj);
  111. if (ret) {
  112. printk(KERN_ERR "failed to allocate framebuffer (%d)\n",
  113. aligned_size);
  114. return -ENOMEM;
  115. }
  116. rbo = gobj->driver_private;
  117. if (fb_tiled)
  118. tiling_flags = RADEON_TILING_MACRO;
  119. #ifdef __BIG_ENDIAN
  120. switch (mode_cmd->bpp) {
  121. case 32:
  122. tiling_flags |= RADEON_TILING_SWAP_32BIT;
  123. break;
  124. case 16:
  125. tiling_flags |= RADEON_TILING_SWAP_16BIT;
  126. default:
  127. break;
  128. }
  129. #endif
  130. if (tiling_flags) {
  131. ret = radeon_bo_set_tiling_flags(rbo,
  132. tiling_flags | RADEON_TILING_SURFACE,
  133. mode_cmd->pitch);
  134. if (ret)
  135. dev_err(rdev->dev, "FB failed to set tiling flags\n");
  136. }
  137. ret = radeon_bo_reserve(rbo, false);
  138. if (unlikely(ret != 0))
  139. goto out_unref;
  140. ret = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, NULL);
  141. if (ret) {
  142. radeon_bo_unreserve(rbo);
  143. goto out_unref;
  144. }
  145. if (fb_tiled)
  146. radeon_bo_check_tiling(rbo, 0, 0);
  147. ret = radeon_bo_kmap(rbo, NULL);
  148. radeon_bo_unreserve(rbo);
  149. if (ret) {
  150. goto out_unref;
  151. }
  152. *gobj_p = gobj;
  153. return 0;
  154. out_unref:
  155. radeonfb_destroy_pinned_object(gobj);
  156. *gobj_p = NULL;
  157. return ret;
  158. }
  159. static int radeonfb_create(struct radeon_fbdev *rfbdev,
  160. struct drm_fb_helper_surface_size *sizes)
  161. {
  162. struct radeon_device *rdev = rfbdev->rdev;
  163. struct fb_info *info;
  164. struct drm_framebuffer *fb = NULL;
  165. struct drm_mode_fb_cmd mode_cmd;
  166. struct drm_gem_object *gobj = NULL;
  167. struct radeon_bo *rbo = NULL;
  168. struct device *device = &rdev->pdev->dev;
  169. int ret;
  170. unsigned long tmp;
  171. mode_cmd.width = sizes->surface_width;
  172. mode_cmd.height = sizes->surface_height;
  173. /* avivo can't scanout real 24bpp */
  174. if ((sizes->surface_bpp == 24) && ASIC_IS_AVIVO(rdev))
  175. sizes->surface_bpp = 32;
  176. mode_cmd.bpp = sizes->surface_bpp;
  177. mode_cmd.depth = sizes->surface_depth;
  178. ret = radeonfb_create_pinned_object(rfbdev, &mode_cmd, &gobj);
  179. rbo = gobj->driver_private;
  180. /* okay we have an object now allocate the framebuffer */
  181. info = framebuffer_alloc(0, device);
  182. if (info == NULL) {
  183. ret = -ENOMEM;
  184. goto out_unref;
  185. }
  186. info->par = rfbdev;
  187. radeon_framebuffer_init(rdev->ddev, &rfbdev->rfb, &mode_cmd, gobj);
  188. fb = &rfbdev->rfb.base;
  189. /* setup helper */
  190. rfbdev->helper.fb = fb;
  191. rfbdev->helper.fbdev = info;
  192. memset_io(rbo->kptr, 0x0, radeon_bo_size(rbo));
  193. strcpy(info->fix.id, "radeondrmfb");
  194. drm_fb_helper_fill_fix(info, fb->pitch, fb->depth);
  195. info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
  196. info->fbops = &radeonfb_ops;
  197. tmp = radeon_bo_gpu_offset(rbo) - rdev->mc.vram_start;
  198. info->fix.smem_start = rdev->mc.aper_base + tmp;
  199. info->fix.smem_len = radeon_bo_size(rbo);
  200. info->screen_base = rbo->kptr;
  201. info->screen_size = radeon_bo_size(rbo);
  202. drm_fb_helper_fill_var(info, &rfbdev->helper, sizes->fb_width, sizes->fb_height);
  203. /* setup aperture base/size for vesafb takeover */
  204. info->apertures = alloc_apertures(1);
  205. if (!info->apertures) {
  206. ret = -ENOMEM;
  207. goto out_unref;
  208. }
  209. info->apertures->ranges[0].base = rdev->ddev->mode_config.fb_base;
  210. info->apertures->ranges[0].size = rdev->mc.real_vram_size;
  211. info->fix.mmio_start = 0;
  212. info->fix.mmio_len = 0;
  213. info->pixmap.size = 64*1024;
  214. info->pixmap.buf_align = 8;
  215. info->pixmap.access_align = 32;
  216. info->pixmap.flags = FB_PIXMAP_SYSTEM;
  217. info->pixmap.scan_align = 1;
  218. if (info->screen_base == NULL) {
  219. ret = -ENOSPC;
  220. goto out_unref;
  221. }
  222. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  223. if (ret) {
  224. ret = -ENOMEM;
  225. goto out_unref;
  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->pitch);
  232. vga_switcheroo_client_fb_set(rdev->ddev->pdev, info);
  233. return 0;
  234. out_unref:
  235. if (rbo) {
  236. }
  237. if (fb && ret) {
  238. drm_gem_object_unreference(gobj);
  239. drm_framebuffer_cleanup(fb);
  240. kfree(fb);
  241. }
  242. return ret;
  243. }
  244. static int radeon_fb_find_or_create_single(struct drm_fb_helper *helper,
  245. struct drm_fb_helper_surface_size *sizes)
  246. {
  247. struct radeon_fbdev *rfbdev = (struct radeon_fbdev *)helper;
  248. int new_fb = 0;
  249. int ret;
  250. if (!helper->fb) {
  251. ret = radeonfb_create(rfbdev, sizes);
  252. if (ret)
  253. return ret;
  254. new_fb = 1;
  255. }
  256. return new_fb;
  257. }
  258. static char *mode_option;
  259. int radeon_parse_options(char *options)
  260. {
  261. char *this_opt;
  262. if (!options || !*options)
  263. return 0;
  264. while ((this_opt = strsep(&options, ",")) != NULL) {
  265. if (!*this_opt)
  266. continue;
  267. mode_option = this_opt;
  268. }
  269. return 0;
  270. }
  271. void radeon_fb_output_poll_changed(struct radeon_device *rdev)
  272. {
  273. drm_fb_helper_hotplug_event(&rdev->mode_info.rfbdev->helper);
  274. }
  275. static int radeon_fbdev_destroy(struct drm_device *dev, struct radeon_fbdev *rfbdev)
  276. {
  277. struct fb_info *info;
  278. struct radeon_framebuffer *rfb = &rfbdev->rfb;
  279. if (rfbdev->helper.fbdev) {
  280. info = rfbdev->helper.fbdev;
  281. unregister_framebuffer(info);
  282. if (info->cmap.len)
  283. fb_dealloc_cmap(&info->cmap);
  284. framebuffer_release(info);
  285. }
  286. if (rfb->obj) {
  287. radeonfb_destroy_pinned_object(rfb->obj);
  288. rfb->obj = NULL;
  289. }
  290. drm_fb_helper_fini(&rfbdev->helper);
  291. drm_framebuffer_cleanup(&rfb->base);
  292. return 0;
  293. }
  294. static struct drm_fb_helper_funcs radeon_fb_helper_funcs = {
  295. .gamma_set = radeon_crtc_fb_gamma_set,
  296. .gamma_get = radeon_crtc_fb_gamma_get,
  297. .fb_probe = radeon_fb_find_or_create_single,
  298. };
  299. int radeon_fbdev_init(struct radeon_device *rdev)
  300. {
  301. struct radeon_fbdev *rfbdev;
  302. int bpp_sel = 32;
  303. int ret;
  304. /* select 8 bpp console on RN50 or 16MB cards */
  305. if (ASIC_IS_RN50(rdev) || rdev->mc.real_vram_size <= (32*1024*1024))
  306. bpp_sel = 8;
  307. rfbdev = kzalloc(sizeof(struct radeon_fbdev), GFP_KERNEL);
  308. if (!rfbdev)
  309. return -ENOMEM;
  310. rfbdev->rdev = rdev;
  311. rdev->mode_info.rfbdev = rfbdev;
  312. rfbdev->helper.funcs = &radeon_fb_helper_funcs;
  313. ret = drm_fb_helper_init(rdev->ddev, &rfbdev->helper,
  314. rdev->num_crtc,
  315. RADEONFB_CONN_LIMIT);
  316. if (ret) {
  317. kfree(rfbdev);
  318. return ret;
  319. }
  320. drm_fb_helper_single_add_all_connectors(&rfbdev->helper);
  321. drm_fb_helper_initial_config(&rfbdev->helper, bpp_sel);
  322. return 0;
  323. }
  324. void radeon_fbdev_fini(struct radeon_device *rdev)
  325. {
  326. if (!rdev->mode_info.rfbdev)
  327. return;
  328. radeon_fbdev_destroy(rdev->ddev, rdev->mode_info.rfbdev);
  329. kfree(rdev->mode_info.rfbdev);
  330. rdev->mode_info.rfbdev = NULL;
  331. }
  332. void radeon_fbdev_set_suspend(struct radeon_device *rdev, int state)
  333. {
  334. fb_set_suspend(rdev->mode_info.rfbdev->helper.fbdev, state);
  335. }
  336. int radeon_fbdev_total_size(struct radeon_device *rdev)
  337. {
  338. struct radeon_bo *robj;
  339. int size = 0;
  340. robj = rdev->mode_info.rfbdev->rfb.obj->driver_private;
  341. size += radeon_bo_size(robj);
  342. return size;
  343. }
  344. bool radeon_fbdev_robj_is_fb(struct radeon_device *rdev, struct radeon_bo *robj)
  345. {
  346. if (robj == rdev->mode_info.rfbdev->rfb.obj->driver_private)
  347. return true;
  348. return false;
  349. }