nouveau_fbcon.c 15 KB

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