nouveau_fbcon.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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/kernel.h>
  28. #include <linux/errno.h>
  29. #include <linux/string.h>
  30. #include <linux/mm.h>
  31. #include <linux/tty.h>
  32. #include <linux/slab.h>
  33. #include <linux/sysrq.h>
  34. #include <linux/delay.h>
  35. #include <linux/fb.h>
  36. #include <linux/init.h>
  37. #include <linux/screen_info.h>
  38. #include "drmP.h"
  39. #include "drm.h"
  40. #include "drm_crtc.h"
  41. #include "drm_crtc_helper.h"
  42. #include "drm_fb_helper.h"
  43. #include "nouveau_drv.h"
  44. #include "nouveau_drm.h"
  45. #include "nouveau_crtc.h"
  46. #include "nouveau_fb.h"
  47. #include "nouveau_fbcon.h"
  48. #include "nouveau_dma.h"
  49. static int
  50. nouveau_fbcon_sync(struct fb_info *info)
  51. {
  52. struct nouveau_fbcon_par *par = info->par;
  53. struct drm_device *dev = par->dev;
  54. struct drm_nouveau_private *dev_priv = dev->dev_private;
  55. struct nouveau_channel *chan = dev_priv->channel;
  56. int ret, i;
  57. if (!chan || !chan->accel_done ||
  58. info->state != FBINFO_STATE_RUNNING ||
  59. info->flags & FBINFO_HWACCEL_DISABLED)
  60. return 0;
  61. if (RING_SPACE(chan, 4)) {
  62. nouveau_fbcon_gpu_lockup(info);
  63. return 0;
  64. }
  65. BEGIN_RING(chan, 0, 0x0104, 1);
  66. OUT_RING(chan, 0);
  67. BEGIN_RING(chan, 0, 0x0100, 1);
  68. OUT_RING(chan, 0);
  69. nouveau_bo_wr32(chan->notifier_bo, chan->m2mf_ntfy + 3, 0xffffffff);
  70. FIRE_RING(chan);
  71. ret = -EBUSY;
  72. for (i = 0; i < 100000; i++) {
  73. if (!nouveau_bo_rd32(chan->notifier_bo, chan->m2mf_ntfy + 3)) {
  74. ret = 0;
  75. break;
  76. }
  77. DRM_UDELAY(1);
  78. }
  79. if (ret) {
  80. nouveau_fbcon_gpu_lockup(info);
  81. return 0;
  82. }
  83. chan->accel_done = false;
  84. return 0;
  85. }
  86. static struct fb_ops nouveau_fbcon_ops = {
  87. .owner = THIS_MODULE,
  88. .fb_check_var = drm_fb_helper_check_var,
  89. .fb_set_par = drm_fb_helper_set_par,
  90. .fb_setcolreg = drm_fb_helper_setcolreg,
  91. .fb_fillrect = cfb_fillrect,
  92. .fb_copyarea = cfb_copyarea,
  93. .fb_imageblit = cfb_imageblit,
  94. .fb_sync = nouveau_fbcon_sync,
  95. .fb_pan_display = drm_fb_helper_pan_display,
  96. .fb_blank = drm_fb_helper_blank,
  97. .fb_setcmap = drm_fb_helper_setcmap,
  98. };
  99. static void nouveau_fbcon_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  100. u16 blue, int regno)
  101. {
  102. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  103. nv_crtc->lut.r[regno] = red;
  104. nv_crtc->lut.g[regno] = green;
  105. nv_crtc->lut.b[regno] = blue;
  106. }
  107. static void nouveau_fbcon_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  108. u16 *blue, int regno)
  109. {
  110. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  111. *red = nv_crtc->lut.r[regno];
  112. *green = nv_crtc->lut.g[regno];
  113. *blue = nv_crtc->lut.b[regno];
  114. }
  115. static struct drm_fb_helper_funcs nouveau_fbcon_helper_funcs = {
  116. .gamma_set = nouveau_fbcon_gamma_set,
  117. .gamma_get = nouveau_fbcon_gamma_get
  118. };
  119. #if defined(__i386__) || defined(__x86_64__)
  120. static bool
  121. nouveau_fbcon_has_vesafb_or_efifb(struct drm_device *dev)
  122. {
  123. struct pci_dev *pdev = dev->pdev;
  124. int ramin;
  125. if (screen_info.orig_video_isVGA != VIDEO_TYPE_VLFB &&
  126. screen_info.orig_video_isVGA != VIDEO_TYPE_EFI)
  127. return false;
  128. if (screen_info.lfb_base < pci_resource_start(pdev, 1))
  129. goto not_fb;
  130. if (screen_info.lfb_base + screen_info.lfb_size >=
  131. pci_resource_start(pdev, 1) + pci_resource_len(pdev, 1))
  132. goto not_fb;
  133. return true;
  134. not_fb:
  135. ramin = 2;
  136. if (pci_resource_len(pdev, ramin) == 0) {
  137. ramin = 3;
  138. if (pci_resource_len(pdev, ramin) == 0)
  139. return false;
  140. }
  141. if (screen_info.lfb_base < pci_resource_start(pdev, ramin))
  142. return false;
  143. if (screen_info.lfb_base + screen_info.lfb_size >=
  144. pci_resource_start(pdev, ramin) + pci_resource_len(pdev, ramin))
  145. return false;
  146. return true;
  147. }
  148. #endif
  149. void
  150. nouveau_fbcon_zfill(struct drm_device *dev)
  151. {
  152. struct drm_nouveau_private *dev_priv = dev->dev_private;
  153. struct fb_info *info = dev_priv->fbdev_info;
  154. struct fb_fillrect rect;
  155. /* Clear the entire fbcon. The drm will program every connector
  156. * with it's preferred mode. If the sizes differ, one display will
  157. * quite likely have garbage around the console.
  158. */
  159. rect.dx = rect.dy = 0;
  160. rect.width = info->var.xres_virtual;
  161. rect.height = info->var.yres_virtual;
  162. rect.color = 0;
  163. rect.rop = ROP_COPY;
  164. info->fbops->fb_fillrect(info, &rect);
  165. }
  166. static int
  167. nouveau_fbcon_create(struct drm_device *dev, uint32_t fb_width,
  168. uint32_t fb_height, uint32_t surface_width,
  169. uint32_t surface_height, uint32_t surface_depth,
  170. uint32_t surface_bpp, struct drm_framebuffer **pfb)
  171. {
  172. struct drm_nouveau_private *dev_priv = dev->dev_private;
  173. struct fb_info *info;
  174. struct nouveau_fbcon_par *par;
  175. struct drm_framebuffer *fb;
  176. struct nouveau_framebuffer *nouveau_fb;
  177. struct nouveau_bo *nvbo;
  178. struct drm_mode_fb_cmd mode_cmd;
  179. struct device *device = &dev->pdev->dev;
  180. int size, ret;
  181. mode_cmd.width = surface_width;
  182. mode_cmd.height = surface_height;
  183. mode_cmd.bpp = surface_bpp;
  184. mode_cmd.pitch = mode_cmd.width * (mode_cmd.bpp >> 3);
  185. mode_cmd.pitch = roundup(mode_cmd.pitch, 256);
  186. mode_cmd.depth = surface_depth;
  187. size = mode_cmd.pitch * mode_cmd.height;
  188. size = roundup(size, PAGE_SIZE);
  189. ret = nouveau_gem_new(dev, dev_priv->channel, size, 0, TTM_PL_FLAG_VRAM,
  190. 0, 0x0000, false, true, &nvbo);
  191. if (ret) {
  192. NV_ERROR(dev, "failed to allocate framebuffer\n");
  193. goto out;
  194. }
  195. ret = nouveau_bo_pin(nvbo, TTM_PL_FLAG_VRAM);
  196. if (ret) {
  197. NV_ERROR(dev, "failed to pin fb: %d\n", ret);
  198. nouveau_bo_ref(NULL, &nvbo);
  199. goto out;
  200. }
  201. ret = nouveau_bo_map(nvbo);
  202. if (ret) {
  203. NV_ERROR(dev, "failed to map fb: %d\n", ret);
  204. nouveau_bo_unpin(nvbo);
  205. nouveau_bo_ref(NULL, &nvbo);
  206. goto out;
  207. }
  208. mutex_lock(&dev->struct_mutex);
  209. fb = nouveau_framebuffer_create(dev, nvbo, &mode_cmd);
  210. if (!fb) {
  211. ret = -ENOMEM;
  212. NV_ERROR(dev, "failed to allocate fb.\n");
  213. goto out_unref;
  214. }
  215. list_add(&fb->filp_head, &dev->mode_config.fb_kernel_list);
  216. nouveau_fb = nouveau_framebuffer(fb);
  217. *pfb = fb;
  218. info = framebuffer_alloc(sizeof(struct nouveau_fbcon_par), device);
  219. if (!info) {
  220. ret = -ENOMEM;
  221. goto out_unref;
  222. }
  223. par = info->par;
  224. par->helper.funcs = &nouveau_fbcon_helper_funcs;
  225. par->helper.dev = dev;
  226. ret = drm_fb_helper_init_crtc_count(&par->helper, 2, 4);
  227. if (ret)
  228. goto out_unref;
  229. dev_priv->fbdev_info = info;
  230. strcpy(info->fix.id, "nouveaufb");
  231. info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_COPYAREA |
  232. FBINFO_HWACCEL_FILLRECT | FBINFO_HWACCEL_IMAGEBLIT;
  233. info->fbops = &nouveau_fbcon_ops;
  234. info->fix.smem_start = dev->mode_config.fb_base + nvbo->bo.offset -
  235. dev_priv->vm_vram_base;
  236. info->fix.smem_len = size;
  237. info->screen_base = nvbo_kmap_obj_iovirtual(nouveau_fb->nvbo);
  238. info->screen_size = size;
  239. drm_fb_helper_fill_fix(info, fb->pitch, fb->depth);
  240. drm_fb_helper_fill_var(info, fb, fb_width, fb_height);
  241. /* FIXME: we really shouldn't expose mmio space at all */
  242. info->fix.mmio_start = pci_resource_start(dev->pdev, 1);
  243. info->fix.mmio_len = pci_resource_len(dev->pdev, 1);
  244. /* Set aperture base/size for vesafb takeover */
  245. #if defined(__i386__) || defined(__x86_64__)
  246. if (nouveau_fbcon_has_vesafb_or_efifb(dev)) {
  247. /* Some NVIDIA VBIOS' are stupid and decide to put the
  248. * framebuffer in the middle of the PRAMIN BAR for
  249. * whatever reason. We need to know the exact lfb_base
  250. * to get vesafb kicked off, and the only reliable way
  251. * we have left is to find out lfb_base the same way
  252. * vesafb did.
  253. */
  254. info->aperture_base = screen_info.lfb_base;
  255. info->aperture_size = screen_info.lfb_size;
  256. if (screen_info.orig_video_isVGA == VIDEO_TYPE_VLFB)
  257. info->aperture_size *= 65536;
  258. } else
  259. #endif
  260. {
  261. info->aperture_base = info->fix.mmio_start;
  262. info->aperture_size = info->fix.mmio_len;
  263. }
  264. info->pixmap.size = 64*1024;
  265. info->pixmap.buf_align = 8;
  266. info->pixmap.access_align = 32;
  267. info->pixmap.flags = FB_PIXMAP_SYSTEM;
  268. info->pixmap.scan_align = 1;
  269. fb->fbdev = info;
  270. par->nouveau_fb = nouveau_fb;
  271. par->dev = dev;
  272. if (dev_priv->channel) {
  273. switch (dev_priv->card_type) {
  274. case NV_50:
  275. nv50_fbcon_accel_init(info);
  276. break;
  277. default:
  278. nv04_fbcon_accel_init(info);
  279. break;
  280. };
  281. }
  282. nouveau_fbcon_zfill(dev);
  283. /* To allow resizeing without swapping buffers */
  284. NV_INFO(dev, "allocated %dx%d fb: 0x%lx, bo %p\n",
  285. nouveau_fb->base.width,
  286. nouveau_fb->base.height,
  287. nvbo->bo.offset, nvbo);
  288. mutex_unlock(&dev->struct_mutex);
  289. return 0;
  290. out_unref:
  291. mutex_unlock(&dev->struct_mutex);
  292. out:
  293. return ret;
  294. }
  295. int
  296. nouveau_fbcon_probe(struct drm_device *dev)
  297. {
  298. NV_DEBUG_KMS(dev, "\n");
  299. return drm_fb_helper_single_fb_probe(dev, 32, nouveau_fbcon_create);
  300. }
  301. int
  302. nouveau_fbcon_remove(struct drm_device *dev, struct drm_framebuffer *fb)
  303. {
  304. struct nouveau_framebuffer *nouveau_fb = nouveau_framebuffer(fb);
  305. struct fb_info *info;
  306. if (!fb)
  307. return -EINVAL;
  308. info = fb->fbdev;
  309. if (info) {
  310. struct nouveau_fbcon_par *par = info->par;
  311. unregister_framebuffer(info);
  312. nouveau_bo_unmap(nouveau_fb->nvbo);
  313. mutex_lock(&dev->struct_mutex);
  314. drm_gem_object_unreference(nouveau_fb->nvbo->gem);
  315. nouveau_fb->nvbo = NULL;
  316. mutex_unlock(&dev->struct_mutex);
  317. if (par)
  318. drm_fb_helper_free(&par->helper);
  319. framebuffer_release(info);
  320. }
  321. return 0;
  322. }
  323. void nouveau_fbcon_gpu_lockup(struct fb_info *info)
  324. {
  325. struct nouveau_fbcon_par *par = info->par;
  326. struct drm_device *dev = par->dev;
  327. NV_ERROR(dev, "GPU lockup - switching to software fbcon\n");
  328. info->flags |= FBINFO_HWACCEL_DISABLED;
  329. }