nouveau_fbcon.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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/sysrq.h>
  33. #include <linux/delay.h>
  34. #include <linux/fb.h>
  35. #include <linux/init.h>
  36. #include <linux/screen_info.h>
  37. #include <linux/vga_switcheroo.h>
  38. #include <linux/console.h>
  39. #include "drmP.h"
  40. #include "drm.h"
  41. #include "drm_crtc.h"
  42. #include "drm_crtc_helper.h"
  43. #include "drm_fb_helper.h"
  44. #include "nouveau_drv.h"
  45. #include "nouveau_drm.h"
  46. #include "nouveau_crtc.h"
  47. #include "nouveau_fb.h"
  48. #include "nouveau_fbcon.h"
  49. #include "nouveau_dma.h"
  50. static void
  51. nouveau_fbcon_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
  52. {
  53. struct nouveau_fbdev *nfbdev = info->par;
  54. struct drm_device *dev = nfbdev->dev;
  55. struct drm_nouveau_private *dev_priv = dev->dev_private;
  56. int ret;
  57. if (info->state != FBINFO_STATE_RUNNING)
  58. return;
  59. ret = -ENODEV;
  60. if (!in_interrupt() && !(info->flags & FBINFO_HWACCEL_DISABLED) &&
  61. mutex_trylock(&dev_priv->channel->mutex)) {
  62. if (dev_priv->card_type < NV_50)
  63. ret = nv04_fbcon_fillrect(info, rect);
  64. else
  65. if (dev_priv->card_type < NV_C0)
  66. ret = nv50_fbcon_fillrect(info, rect);
  67. else
  68. ret = nvc0_fbcon_fillrect(info, rect);
  69. mutex_unlock(&dev_priv->channel->mutex);
  70. }
  71. if (ret == 0)
  72. return;
  73. if (ret != -ENODEV)
  74. nouveau_fbcon_gpu_lockup(info);
  75. cfb_fillrect(info, rect);
  76. }
  77. static void
  78. nouveau_fbcon_copyarea(struct fb_info *info, const struct fb_copyarea *image)
  79. {
  80. struct nouveau_fbdev *nfbdev = info->par;
  81. struct drm_device *dev = nfbdev->dev;
  82. struct drm_nouveau_private *dev_priv = dev->dev_private;
  83. int ret;
  84. if (info->state != FBINFO_STATE_RUNNING)
  85. return;
  86. ret = -ENODEV;
  87. if (!in_interrupt() && !(info->flags & FBINFO_HWACCEL_DISABLED) &&
  88. mutex_trylock(&dev_priv->channel->mutex)) {
  89. if (dev_priv->card_type < NV_50)
  90. ret = nv04_fbcon_copyarea(info, image);
  91. else
  92. if (dev_priv->card_type < NV_C0)
  93. ret = nv50_fbcon_copyarea(info, image);
  94. else
  95. ret = nvc0_fbcon_copyarea(info, image);
  96. mutex_unlock(&dev_priv->channel->mutex);
  97. }
  98. if (ret == 0)
  99. return;
  100. if (ret != -ENODEV)
  101. nouveau_fbcon_gpu_lockup(info);
  102. cfb_copyarea(info, image);
  103. }
  104. static void
  105. nouveau_fbcon_imageblit(struct fb_info *info, const struct fb_image *image)
  106. {
  107. struct nouveau_fbdev *nfbdev = info->par;
  108. struct drm_device *dev = nfbdev->dev;
  109. struct drm_nouveau_private *dev_priv = dev->dev_private;
  110. int ret;
  111. if (info->state != FBINFO_STATE_RUNNING)
  112. return;
  113. ret = -ENODEV;
  114. if (!in_interrupt() && !(info->flags & FBINFO_HWACCEL_DISABLED) &&
  115. mutex_trylock(&dev_priv->channel->mutex)) {
  116. if (dev_priv->card_type < NV_50)
  117. ret = nv04_fbcon_imageblit(info, image);
  118. else
  119. if (dev_priv->card_type < NV_C0)
  120. ret = nv50_fbcon_imageblit(info, image);
  121. else
  122. ret = nvc0_fbcon_imageblit(info, image);
  123. mutex_unlock(&dev_priv->channel->mutex);
  124. }
  125. if (ret == 0)
  126. return;
  127. if (ret != -ENODEV)
  128. nouveau_fbcon_gpu_lockup(info);
  129. cfb_imageblit(info, image);
  130. }
  131. static int
  132. nouveau_fbcon_sync(struct fb_info *info)
  133. {
  134. struct nouveau_fbdev *nfbdev = info->par;
  135. struct drm_device *dev = nfbdev->dev;
  136. struct drm_nouveau_private *dev_priv = dev->dev_private;
  137. struct nouveau_channel *chan = dev_priv->channel;
  138. int ret;
  139. if (!chan || !chan->accel_done || in_interrupt() ||
  140. info->state != FBINFO_STATE_RUNNING ||
  141. info->flags & FBINFO_HWACCEL_DISABLED)
  142. return 0;
  143. if (!mutex_trylock(&chan->mutex))
  144. return 0;
  145. ret = nouveau_channel_idle(chan);
  146. mutex_unlock(&chan->mutex);
  147. if (ret) {
  148. nouveau_fbcon_gpu_lockup(info);
  149. return 0;
  150. }
  151. chan->accel_done = false;
  152. return 0;
  153. }
  154. static struct fb_ops nouveau_fbcon_ops = {
  155. .owner = THIS_MODULE,
  156. .fb_check_var = drm_fb_helper_check_var,
  157. .fb_set_par = drm_fb_helper_set_par,
  158. .fb_fillrect = nouveau_fbcon_fillrect,
  159. .fb_copyarea = nouveau_fbcon_copyarea,
  160. .fb_imageblit = nouveau_fbcon_imageblit,
  161. .fb_sync = nouveau_fbcon_sync,
  162. .fb_pan_display = drm_fb_helper_pan_display,
  163. .fb_blank = drm_fb_helper_blank,
  164. .fb_setcmap = drm_fb_helper_setcmap,
  165. .fb_debug_enter = drm_fb_helper_debug_enter,
  166. .fb_debug_leave = drm_fb_helper_debug_leave,
  167. };
  168. static struct fb_ops nouveau_fbcon_sw_ops = {
  169. .owner = THIS_MODULE,
  170. .fb_check_var = drm_fb_helper_check_var,
  171. .fb_set_par = drm_fb_helper_set_par,
  172. .fb_fillrect = cfb_fillrect,
  173. .fb_copyarea = cfb_copyarea,
  174. .fb_imageblit = cfb_imageblit,
  175. .fb_pan_display = drm_fb_helper_pan_display,
  176. .fb_blank = drm_fb_helper_blank,
  177. .fb_setcmap = drm_fb_helper_setcmap,
  178. .fb_debug_enter = drm_fb_helper_debug_enter,
  179. .fb_debug_leave = drm_fb_helper_debug_leave,
  180. };
  181. static void nouveau_fbcon_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  182. u16 blue, int regno)
  183. {
  184. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  185. nv_crtc->lut.r[regno] = red;
  186. nv_crtc->lut.g[regno] = green;
  187. nv_crtc->lut.b[regno] = blue;
  188. }
  189. static void nouveau_fbcon_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  190. u16 *blue, int regno)
  191. {
  192. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  193. *red = nv_crtc->lut.r[regno];
  194. *green = nv_crtc->lut.g[regno];
  195. *blue = nv_crtc->lut.b[regno];
  196. }
  197. static void
  198. nouveau_fbcon_zfill(struct drm_device *dev, struct nouveau_fbdev *nfbdev)
  199. {
  200. struct fb_info *info = nfbdev->helper.fbdev;
  201. struct fb_fillrect rect;
  202. /* Clear the entire fbcon. The drm will program every connector
  203. * with it's preferred mode. If the sizes differ, one display will
  204. * quite likely have garbage around the console.
  205. */
  206. rect.dx = rect.dy = 0;
  207. rect.width = info->var.xres_virtual;
  208. rect.height = info->var.yres_virtual;
  209. rect.color = 0;
  210. rect.rop = ROP_COPY;
  211. info->fbops->fb_fillrect(info, &rect);
  212. }
  213. static int
  214. nouveau_fbcon_create(struct nouveau_fbdev *nfbdev,
  215. struct drm_fb_helper_surface_size *sizes)
  216. {
  217. struct drm_device *dev = nfbdev->dev;
  218. struct drm_nouveau_private *dev_priv = dev->dev_private;
  219. struct fb_info *info;
  220. struct drm_framebuffer *fb;
  221. struct nouveau_framebuffer *nouveau_fb;
  222. struct nouveau_channel *chan;
  223. struct nouveau_bo *nvbo;
  224. struct drm_mode_fb_cmd2 mode_cmd;
  225. struct pci_dev *pdev = dev->pdev;
  226. struct device *device = &pdev->dev;
  227. int size, ret;
  228. mode_cmd.width = sizes->surface_width;
  229. mode_cmd.height = sizes->surface_height;
  230. mode_cmd.pitches[0] = mode_cmd.width * (sizes->surface_bpp >> 3);
  231. mode_cmd.pitches[0] = roundup(mode_cmd.pitches[0], 256);
  232. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  233. sizes->surface_depth);
  234. size = mode_cmd.pitches[0] * mode_cmd.height;
  235. size = roundup(size, PAGE_SIZE);
  236. ret = nouveau_gem_new(dev, size, 0, NOUVEAU_GEM_DOMAIN_VRAM,
  237. 0, 0x0000, &nvbo);
  238. if (ret) {
  239. NV_ERROR(dev, "failed to allocate framebuffer\n");
  240. goto out;
  241. }
  242. ret = nouveau_bo_pin(nvbo, TTM_PL_FLAG_VRAM);
  243. if (ret) {
  244. NV_ERROR(dev, "failed to pin fb: %d\n", ret);
  245. nouveau_bo_ref(NULL, &nvbo);
  246. goto out;
  247. }
  248. ret = nouveau_bo_map(nvbo);
  249. if (ret) {
  250. NV_ERROR(dev, "failed to map fb: %d\n", ret);
  251. nouveau_bo_unpin(nvbo);
  252. nouveau_bo_ref(NULL, &nvbo);
  253. goto out;
  254. }
  255. chan = nouveau_nofbaccel ? NULL : dev_priv->channel;
  256. if (chan && dev_priv->card_type >= NV_50) {
  257. ret = nouveau_bo_vma_add(nvbo, chan->vm, &nfbdev->nouveau_fb.vma);
  258. if (ret) {
  259. NV_ERROR(dev, "failed to map fb into chan: %d\n", ret);
  260. chan = NULL;
  261. }
  262. }
  263. mutex_lock(&dev->struct_mutex);
  264. info = framebuffer_alloc(0, device);
  265. if (!info) {
  266. ret = -ENOMEM;
  267. goto out_unref;
  268. }
  269. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  270. if (ret) {
  271. ret = -ENOMEM;
  272. goto out_unref;
  273. }
  274. info->par = nfbdev;
  275. nouveau_framebuffer_init(dev, &nfbdev->nouveau_fb, &mode_cmd, nvbo);
  276. nouveau_fb = &nfbdev->nouveau_fb;
  277. fb = &nouveau_fb->base;
  278. /* setup helper */
  279. nfbdev->helper.fb = fb;
  280. nfbdev->helper.fbdev = info;
  281. strcpy(info->fix.id, "nouveaufb");
  282. if (nouveau_nofbaccel)
  283. info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_DISABLED;
  284. else
  285. info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_COPYAREA |
  286. FBINFO_HWACCEL_FILLRECT |
  287. FBINFO_HWACCEL_IMAGEBLIT;
  288. info->flags |= FBINFO_CAN_FORCE_OUTPUT;
  289. info->fbops = &nouveau_fbcon_sw_ops;
  290. info->fix.smem_start = nvbo->bo.mem.bus.base +
  291. nvbo->bo.mem.bus.offset;
  292. info->fix.smem_len = size;
  293. info->screen_base = nvbo_kmap_obj_iovirtual(nouveau_fb->nvbo);
  294. info->screen_size = size;
  295. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  296. drm_fb_helper_fill_var(info, &nfbdev->helper, sizes->fb_width, sizes->fb_height);
  297. /* Set aperture base/size for vesafb takeover */
  298. info->apertures = dev_priv->apertures;
  299. if (!info->apertures) {
  300. ret = -ENOMEM;
  301. goto out_unref;
  302. }
  303. /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
  304. mutex_unlock(&dev->struct_mutex);
  305. if (dev_priv->channel && !nouveau_nofbaccel) {
  306. ret = -ENODEV;
  307. if (dev_priv->card_type < NV_50)
  308. ret = nv04_fbcon_accel_init(info);
  309. else
  310. if (dev_priv->card_type < NV_C0)
  311. ret = nv50_fbcon_accel_init(info);
  312. else
  313. ret = nvc0_fbcon_accel_init(info);
  314. if (ret == 0)
  315. info->fbops = &nouveau_fbcon_ops;
  316. }
  317. nouveau_fbcon_zfill(dev, nfbdev);
  318. /* To allow resizeing without swapping buffers */
  319. NV_INFO(dev, "allocated %dx%d fb: 0x%lx, bo %p\n",
  320. nouveau_fb->base.width,
  321. nouveau_fb->base.height,
  322. nvbo->bo.offset, nvbo);
  323. vga_switcheroo_client_fb_set(dev->pdev, info);
  324. return 0;
  325. out_unref:
  326. mutex_unlock(&dev->struct_mutex);
  327. out:
  328. return ret;
  329. }
  330. static int
  331. nouveau_fbcon_find_or_create_single(struct drm_fb_helper *helper,
  332. struct drm_fb_helper_surface_size *sizes)
  333. {
  334. struct nouveau_fbdev *nfbdev = (struct nouveau_fbdev *)helper;
  335. int new_fb = 0;
  336. int ret;
  337. if (!helper->fb) {
  338. ret = nouveau_fbcon_create(nfbdev, sizes);
  339. if (ret)
  340. return ret;
  341. new_fb = 1;
  342. }
  343. return new_fb;
  344. }
  345. void
  346. nouveau_fbcon_output_poll_changed(struct drm_device *dev)
  347. {
  348. struct drm_nouveau_private *dev_priv = dev->dev_private;
  349. drm_fb_helper_hotplug_event(&dev_priv->nfbdev->helper);
  350. }
  351. static int
  352. nouveau_fbcon_destroy(struct drm_device *dev, struct nouveau_fbdev *nfbdev)
  353. {
  354. struct nouveau_framebuffer *nouveau_fb = &nfbdev->nouveau_fb;
  355. struct fb_info *info;
  356. if (nfbdev->helper.fbdev) {
  357. info = nfbdev->helper.fbdev;
  358. unregister_framebuffer(info);
  359. if (info->cmap.len)
  360. fb_dealloc_cmap(&info->cmap);
  361. framebuffer_release(info);
  362. }
  363. if (nouveau_fb->nvbo) {
  364. nouveau_bo_unmap(nouveau_fb->nvbo);
  365. nouveau_bo_vma_del(nouveau_fb->nvbo, &nouveau_fb->vma);
  366. drm_gem_object_unreference_unlocked(nouveau_fb->nvbo->gem);
  367. nouveau_fb->nvbo = NULL;
  368. }
  369. drm_fb_helper_fini(&nfbdev->helper);
  370. drm_framebuffer_cleanup(&nouveau_fb->base);
  371. return 0;
  372. }
  373. void nouveau_fbcon_gpu_lockup(struct fb_info *info)
  374. {
  375. struct nouveau_fbdev *nfbdev = info->par;
  376. struct drm_device *dev = nfbdev->dev;
  377. NV_ERROR(dev, "GPU lockup - switching to software fbcon\n");
  378. info->flags |= FBINFO_HWACCEL_DISABLED;
  379. }
  380. static struct drm_fb_helper_funcs nouveau_fbcon_helper_funcs = {
  381. .gamma_set = nouveau_fbcon_gamma_set,
  382. .gamma_get = nouveau_fbcon_gamma_get,
  383. .fb_probe = nouveau_fbcon_find_or_create_single,
  384. };
  385. int nouveau_fbcon_init(struct drm_device *dev)
  386. {
  387. struct drm_nouveau_private *dev_priv = dev->dev_private;
  388. struct nouveau_fbdev *nfbdev;
  389. int preferred_bpp;
  390. int ret;
  391. nfbdev = kzalloc(sizeof(struct nouveau_fbdev), GFP_KERNEL);
  392. if (!nfbdev)
  393. return -ENOMEM;
  394. nfbdev->dev = dev;
  395. dev_priv->nfbdev = nfbdev;
  396. nfbdev->helper.funcs = &nouveau_fbcon_helper_funcs;
  397. ret = drm_fb_helper_init(dev, &nfbdev->helper,
  398. dev->mode_config.num_crtc, 4);
  399. if (ret) {
  400. kfree(nfbdev);
  401. return ret;
  402. }
  403. drm_fb_helper_single_add_all_connectors(&nfbdev->helper);
  404. if (dev_priv->vram_size <= 32 * 1024 * 1024)
  405. preferred_bpp = 8;
  406. else if (dev_priv->vram_size <= 64 * 1024 * 1024)
  407. preferred_bpp = 16;
  408. else
  409. preferred_bpp = 32;
  410. drm_fb_helper_initial_config(&nfbdev->helper, preferred_bpp);
  411. return 0;
  412. }
  413. void nouveau_fbcon_fini(struct drm_device *dev)
  414. {
  415. struct drm_nouveau_private *dev_priv = dev->dev_private;
  416. if (!dev_priv->nfbdev)
  417. return;
  418. nouveau_fbcon_destroy(dev, dev_priv->nfbdev);
  419. kfree(dev_priv->nfbdev);
  420. dev_priv->nfbdev = NULL;
  421. }
  422. void nouveau_fbcon_save_disable_accel(struct drm_device *dev)
  423. {
  424. struct drm_nouveau_private *dev_priv = dev->dev_private;
  425. dev_priv->nfbdev->saved_flags = dev_priv->nfbdev->helper.fbdev->flags;
  426. dev_priv->nfbdev->helper.fbdev->flags |= FBINFO_HWACCEL_DISABLED;
  427. }
  428. void nouveau_fbcon_restore_accel(struct drm_device *dev)
  429. {
  430. struct drm_nouveau_private *dev_priv = dev->dev_private;
  431. dev_priv->nfbdev->helper.fbdev->flags = dev_priv->nfbdev->saved_flags;
  432. }
  433. void nouveau_fbcon_set_suspend(struct drm_device *dev, int state)
  434. {
  435. struct drm_nouveau_private *dev_priv = dev->dev_private;
  436. console_lock();
  437. if (state == 0)
  438. nouveau_fbcon_save_disable_accel(dev);
  439. fb_set_suspend(dev_priv->nfbdev->helper.fbdev, state);
  440. if (state == 1)
  441. nouveau_fbcon_restore_accel(dev);
  442. console_unlock();
  443. }
  444. void nouveau_fbcon_zfill_all(struct drm_device *dev)
  445. {
  446. struct drm_nouveau_private *dev_priv = dev->dev_private;
  447. nouveau_fbcon_zfill(dev, dev_priv->nfbdev);
  448. }