radeon_fb.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. /*
  27. * Modularization
  28. */
  29. #include <linux/module.h>
  30. #include <linux/slab.h>
  31. #include <linux/fb.h>
  32. #include "drmP.h"
  33. #include "drm.h"
  34. #include "drm_crtc.h"
  35. #include "drm_crtc_helper.h"
  36. #include "radeon_drm.h"
  37. #include "radeon.h"
  38. #include "drm_fb_helper.h"
  39. #include <linux/vga_switcheroo.h>
  40. struct radeon_fb_device {
  41. struct drm_fb_helper helper;
  42. struct radeon_framebuffer *rfb;
  43. struct radeon_device *rdev;
  44. };
  45. static struct fb_ops radeonfb_ops = {
  46. .owner = THIS_MODULE,
  47. .fb_check_var = drm_fb_helper_check_var,
  48. .fb_set_par = drm_fb_helper_set_par,
  49. .fb_setcolreg = drm_fb_helper_setcolreg,
  50. .fb_fillrect = cfb_fillrect,
  51. .fb_copyarea = cfb_copyarea,
  52. .fb_imageblit = 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. };
  57. /**
  58. * Currently it is assumed that the old framebuffer is reused.
  59. *
  60. * LOCKING
  61. * caller should hold the mode config lock.
  62. *
  63. */
  64. int radeonfb_resize(struct drm_device *dev, struct drm_crtc *crtc)
  65. {
  66. struct fb_info *info;
  67. struct drm_framebuffer *fb;
  68. struct drm_display_mode *mode = crtc->desired_mode;
  69. fb = crtc->fb;
  70. if (fb == NULL) {
  71. return 1;
  72. }
  73. info = fb->fbdev;
  74. if (info == NULL) {
  75. return 1;
  76. }
  77. if (mode == NULL) {
  78. return 1;
  79. }
  80. info->var.xres = mode->hdisplay;
  81. info->var.right_margin = mode->hsync_start - mode->hdisplay;
  82. info->var.hsync_len = mode->hsync_end - mode->hsync_start;
  83. info->var.left_margin = mode->htotal - mode->hsync_end;
  84. info->var.yres = mode->vdisplay;
  85. info->var.lower_margin = mode->vsync_start - mode->vdisplay;
  86. info->var.vsync_len = mode->vsync_end - mode->vsync_start;
  87. info->var.upper_margin = mode->vtotal - mode->vsync_end;
  88. info->var.pixclock = 10000000 / mode->htotal * 1000 / mode->vtotal * 100;
  89. /* avoid overflow */
  90. info->var.pixclock = info->var.pixclock * 1000 / mode->vrefresh;
  91. return 0;
  92. }
  93. EXPORT_SYMBOL(radeonfb_resize);
  94. static int radeon_align_pitch(struct radeon_device *rdev, int width, int bpp, bool tiled)
  95. {
  96. int aligned = width;
  97. int align_large = (ASIC_IS_AVIVO(rdev)) || tiled;
  98. int pitch_mask = 0;
  99. switch (bpp / 8) {
  100. case 1:
  101. pitch_mask = align_large ? 255 : 127;
  102. break;
  103. case 2:
  104. pitch_mask = align_large ? 127 : 31;
  105. break;
  106. case 3:
  107. case 4:
  108. pitch_mask = align_large ? 63 : 15;
  109. break;
  110. }
  111. aligned += pitch_mask;
  112. aligned &= ~pitch_mask;
  113. return aligned;
  114. }
  115. static struct drm_fb_helper_funcs radeon_fb_helper_funcs = {
  116. .gamma_set = radeon_crtc_fb_gamma_set,
  117. .gamma_get = radeon_crtc_fb_gamma_get,
  118. };
  119. int radeonfb_create(struct drm_device *dev,
  120. uint32_t fb_width, uint32_t fb_height,
  121. uint32_t surface_width, uint32_t surface_height,
  122. uint32_t surface_depth, uint32_t surface_bpp,
  123. struct drm_framebuffer **fb_p)
  124. {
  125. struct radeon_device *rdev = dev->dev_private;
  126. struct fb_info *info;
  127. struct radeon_fb_device *rfbdev;
  128. struct drm_framebuffer *fb = NULL;
  129. struct radeon_framebuffer *rfb;
  130. struct drm_mode_fb_cmd mode_cmd;
  131. struct drm_gem_object *gobj = NULL;
  132. struct radeon_bo *rbo = NULL;
  133. struct device *device = &rdev->pdev->dev;
  134. int size, aligned_size, ret;
  135. u64 fb_gpuaddr;
  136. void *fbptr = NULL;
  137. unsigned long tmp;
  138. bool fb_tiled = false; /* useful for testing */
  139. u32 tiling_flags = 0;
  140. mode_cmd.width = surface_width;
  141. mode_cmd.height = surface_height;
  142. /* avivo can't scanout real 24bpp */
  143. if ((surface_bpp == 24) && ASIC_IS_AVIVO(rdev))
  144. surface_bpp = 32;
  145. mode_cmd.bpp = surface_bpp;
  146. /* need to align pitch with crtc limits */
  147. mode_cmd.pitch = radeon_align_pitch(rdev, mode_cmd.width, mode_cmd.bpp, fb_tiled) * ((mode_cmd.bpp + 1) / 8);
  148. mode_cmd.depth = surface_depth;
  149. size = mode_cmd.pitch * mode_cmd.height;
  150. aligned_size = ALIGN(size, PAGE_SIZE);
  151. ret = radeon_gem_object_create(rdev, aligned_size, 0,
  152. RADEON_GEM_DOMAIN_VRAM,
  153. false, ttm_bo_type_kernel,
  154. &gobj);
  155. if (ret) {
  156. printk(KERN_ERR "failed to allocate framebuffer (%d %d)\n",
  157. surface_width, surface_height);
  158. ret = -ENOMEM;
  159. goto out;
  160. }
  161. rbo = gobj->driver_private;
  162. if (fb_tiled)
  163. tiling_flags = RADEON_TILING_MACRO;
  164. #ifdef __BIG_ENDIAN
  165. switch (mode_cmd.bpp) {
  166. case 32:
  167. tiling_flags |= RADEON_TILING_SWAP_32BIT;
  168. break;
  169. case 16:
  170. tiling_flags |= RADEON_TILING_SWAP_16BIT;
  171. default:
  172. break;
  173. }
  174. #endif
  175. if (tiling_flags) {
  176. ret = radeon_bo_set_tiling_flags(rbo,
  177. tiling_flags | RADEON_TILING_SURFACE,
  178. mode_cmd.pitch);
  179. if (ret)
  180. dev_err(rdev->dev, "FB failed to set tiling flags\n");
  181. }
  182. mutex_lock(&rdev->ddev->struct_mutex);
  183. fb = radeon_framebuffer_create(rdev->ddev, &mode_cmd, gobj);
  184. if (fb == NULL) {
  185. DRM_ERROR("failed to allocate fb.\n");
  186. ret = -ENOMEM;
  187. goto out_unref;
  188. }
  189. ret = radeon_bo_reserve(rbo, false);
  190. if (unlikely(ret != 0))
  191. goto out_unref;
  192. ret = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_gpuaddr);
  193. if (ret) {
  194. radeon_bo_unreserve(rbo);
  195. goto out_unref;
  196. }
  197. if (fb_tiled)
  198. radeon_bo_check_tiling(rbo, 0, 0);
  199. ret = radeon_bo_kmap(rbo, &fbptr);
  200. radeon_bo_unreserve(rbo);
  201. if (ret) {
  202. goto out_unref;
  203. }
  204. list_add(&fb->filp_head, &rdev->ddev->mode_config.fb_kernel_list);
  205. *fb_p = fb;
  206. rfb = to_radeon_framebuffer(fb);
  207. rdev->fbdev_rfb = rfb;
  208. rdev->fbdev_rbo = rbo;
  209. info = framebuffer_alloc(sizeof(struct radeon_fb_device), device);
  210. if (info == NULL) {
  211. ret = -ENOMEM;
  212. goto out_unref;
  213. }
  214. rdev->fbdev_info = info;
  215. rfbdev = info->par;
  216. rfbdev->helper.funcs = &radeon_fb_helper_funcs;
  217. rfbdev->helper.dev = dev;
  218. ret = drm_fb_helper_init_crtc_count(&rfbdev->helper, rdev->num_crtc,
  219. RADEONFB_CONN_LIMIT);
  220. if (ret)
  221. goto out_unref;
  222. memset_io(fbptr, 0x0, aligned_size);
  223. strcpy(info->fix.id, "radeondrmfb");
  224. drm_fb_helper_fill_fix(info, fb->pitch, fb->depth);
  225. info->flags = FBINFO_DEFAULT;
  226. info->fbops = &radeonfb_ops;
  227. tmp = fb_gpuaddr - rdev->mc.vram_start;
  228. info->fix.smem_start = rdev->mc.aper_base + tmp;
  229. info->fix.smem_len = size;
  230. info->screen_base = fbptr;
  231. info->screen_size = size;
  232. drm_fb_helper_fill_var(info, fb, fb_width, fb_height);
  233. /* setup aperture base/size for vesafb takeover */
  234. info->aperture_base = rdev->ddev->mode_config.fb_base;
  235. info->aperture_size = rdev->mc.real_vram_size;
  236. info->fix.mmio_start = 0;
  237. info->fix.mmio_len = 0;
  238. info->pixmap.size = 64*1024;
  239. info->pixmap.buf_align = 8;
  240. info->pixmap.access_align = 32;
  241. info->pixmap.flags = FB_PIXMAP_SYSTEM;
  242. info->pixmap.scan_align = 1;
  243. if (info->screen_base == NULL) {
  244. ret = -ENOSPC;
  245. goto out_unref;
  246. }
  247. DRM_INFO("fb mappable at 0x%lX\n", info->fix.smem_start);
  248. DRM_INFO("vram apper at 0x%lX\n", (unsigned long)rdev->mc.aper_base);
  249. DRM_INFO("size %lu\n", (unsigned long)size);
  250. DRM_INFO("fb depth is %d\n", fb->depth);
  251. DRM_INFO(" pitch is %d\n", fb->pitch);
  252. fb->fbdev = info;
  253. rfbdev->rfb = rfb;
  254. rfbdev->rdev = rdev;
  255. mutex_unlock(&rdev->ddev->struct_mutex);
  256. vga_switcheroo_client_fb_set(rdev->ddev->pdev, info);
  257. return 0;
  258. out_unref:
  259. if (rbo) {
  260. ret = radeon_bo_reserve(rbo, false);
  261. if (likely(ret == 0)) {
  262. radeon_bo_kunmap(rbo);
  263. radeon_bo_unreserve(rbo);
  264. }
  265. }
  266. if (fb && ret) {
  267. list_del(&fb->filp_head);
  268. drm_gem_object_unreference(gobj);
  269. drm_framebuffer_cleanup(fb);
  270. kfree(fb);
  271. }
  272. drm_gem_object_unreference(gobj);
  273. mutex_unlock(&rdev->ddev->struct_mutex);
  274. out:
  275. return ret;
  276. }
  277. static char *mode_option;
  278. int radeon_parse_options(char *options)
  279. {
  280. char *this_opt;
  281. if (!options || !*options)
  282. return 0;
  283. while ((this_opt = strsep(&options, ",")) != NULL) {
  284. if (!*this_opt)
  285. continue;
  286. mode_option = this_opt;
  287. }
  288. return 0;
  289. }
  290. int radeonfb_probe(struct drm_device *dev)
  291. {
  292. struct radeon_device *rdev = dev->dev_private;
  293. int bpp_sel = 32;
  294. /* select 8 bpp console on RN50 or 16MB cards */
  295. if (ASIC_IS_RN50(rdev) || rdev->mc.real_vram_size <= (32*1024*1024))
  296. bpp_sel = 8;
  297. return drm_fb_helper_single_fb_probe(dev, bpp_sel, &radeonfb_create);
  298. }
  299. int radeonfb_remove(struct drm_device *dev, struct drm_framebuffer *fb)
  300. {
  301. struct fb_info *info;
  302. struct radeon_framebuffer *rfb = to_radeon_framebuffer(fb);
  303. struct radeon_bo *rbo;
  304. int r;
  305. if (!fb) {
  306. return -EINVAL;
  307. }
  308. info = fb->fbdev;
  309. if (info) {
  310. struct radeon_fb_device *rfbdev = info->par;
  311. rbo = rfb->obj->driver_private;
  312. unregister_framebuffer(info);
  313. r = radeon_bo_reserve(rbo, false);
  314. if (likely(r == 0)) {
  315. radeon_bo_kunmap(rbo);
  316. radeon_bo_unpin(rbo);
  317. radeon_bo_unreserve(rbo);
  318. }
  319. drm_fb_helper_free(&rfbdev->helper);
  320. framebuffer_release(info);
  321. }
  322. printk(KERN_INFO "unregistered panic notifier\n");
  323. return 0;
  324. }
  325. EXPORT_SYMBOL(radeonfb_remove);
  326. MODULE_LICENSE("GPL");