radeon_fb.c 8.8 KB

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