nouveau_fbcon.c 14 KB

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