nouveau_display.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /*
  2. * Copyright (C) 2008 Maarten Maathuis.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial
  15. * portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. */
  26. #include <drm/drmP.h>
  27. #include <drm/drm_crtc_helper.h>
  28. #include "nouveau_fbcon.h"
  29. #include "dispnv04/hw.h"
  30. #include "nouveau_crtc.h"
  31. #include "nouveau_dma.h"
  32. #include "nouveau_gem.h"
  33. #include "nouveau_connector.h"
  34. #include "nv50_display.h"
  35. #include "nouveau_fence.h"
  36. #include <engine/disp.h>
  37. #include <core/class.h>
  38. static int
  39. nouveau_display_vblank_handler(void *data, int head)
  40. {
  41. struct nouveau_drm *drm = data;
  42. drm_handle_vblank(drm->dev, head);
  43. return NVKM_EVENT_KEEP;
  44. }
  45. int
  46. nouveau_display_vblank_enable(struct drm_device *dev, int head)
  47. {
  48. struct nouveau_display *disp = nouveau_display(dev);
  49. if (disp) {
  50. nouveau_event_get(disp->vblank[head]);
  51. return 0;
  52. }
  53. return -EIO;
  54. }
  55. void
  56. nouveau_display_vblank_disable(struct drm_device *dev, int head)
  57. {
  58. struct nouveau_display *disp = nouveau_display(dev);
  59. if (disp)
  60. nouveau_event_put(disp->vblank[head]);
  61. }
  62. static void
  63. nouveau_display_vblank_fini(struct drm_device *dev)
  64. {
  65. struct nouveau_display *disp = nouveau_display(dev);
  66. int i;
  67. if (disp->vblank) {
  68. for (i = 0; i < dev->mode_config.num_crtc; i++)
  69. nouveau_event_ref(NULL, &disp->vblank[i]);
  70. kfree(disp->vblank);
  71. disp->vblank = NULL;
  72. }
  73. drm_vblank_cleanup(dev);
  74. }
  75. static int
  76. nouveau_display_vblank_init(struct drm_device *dev)
  77. {
  78. struct nouveau_display *disp = nouveau_display(dev);
  79. struct nouveau_drm *drm = nouveau_drm(dev);
  80. struct nouveau_disp *pdisp = nouveau_disp(drm->device);
  81. int ret, i;
  82. disp->vblank = kzalloc(dev->mode_config.num_crtc *
  83. sizeof(*disp->vblank), GFP_KERNEL);
  84. if (!disp->vblank)
  85. return -ENOMEM;
  86. for (i = 0; i < dev->mode_config.num_crtc; i++) {
  87. ret = nouveau_event_new(pdisp->vblank, i,
  88. nouveau_display_vblank_handler,
  89. drm, &disp->vblank[i]);
  90. if (ret) {
  91. nouveau_display_vblank_fini(dev);
  92. return ret;
  93. }
  94. }
  95. ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
  96. if (ret) {
  97. nouveau_display_vblank_fini(dev);
  98. return ret;
  99. }
  100. return 0;
  101. }
  102. static void
  103. nouveau_user_framebuffer_destroy(struct drm_framebuffer *drm_fb)
  104. {
  105. struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
  106. if (fb->nvbo)
  107. drm_gem_object_unreference_unlocked(&fb->nvbo->gem);
  108. drm_framebuffer_cleanup(drm_fb);
  109. kfree(fb);
  110. }
  111. static int
  112. nouveau_user_framebuffer_create_handle(struct drm_framebuffer *drm_fb,
  113. struct drm_file *file_priv,
  114. unsigned int *handle)
  115. {
  116. struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
  117. return drm_gem_handle_create(file_priv, &fb->nvbo->gem, handle);
  118. }
  119. static const struct drm_framebuffer_funcs nouveau_framebuffer_funcs = {
  120. .destroy = nouveau_user_framebuffer_destroy,
  121. .create_handle = nouveau_user_framebuffer_create_handle,
  122. };
  123. int
  124. nouveau_framebuffer_init(struct drm_device *dev,
  125. struct nouveau_framebuffer *nv_fb,
  126. struct drm_mode_fb_cmd2 *mode_cmd,
  127. struct nouveau_bo *nvbo)
  128. {
  129. struct nouveau_drm *drm = nouveau_drm(dev);
  130. struct drm_framebuffer *fb = &nv_fb->base;
  131. int ret;
  132. drm_helper_mode_fill_fb_struct(fb, mode_cmd);
  133. nv_fb->nvbo = nvbo;
  134. if (nv_device(drm->device)->card_type >= NV_50) {
  135. u32 tile_flags = nouveau_bo_tile_layout(nvbo);
  136. if (tile_flags == 0x7a00 ||
  137. tile_flags == 0xfe00)
  138. nv_fb->r_dma = NvEvoFB32;
  139. else
  140. if (tile_flags == 0x7000)
  141. nv_fb->r_dma = NvEvoFB16;
  142. else
  143. nv_fb->r_dma = NvEvoVRAM_LP;
  144. switch (fb->depth) {
  145. case 8: nv_fb->r_format = 0x1e00; break;
  146. case 15: nv_fb->r_format = 0xe900; break;
  147. case 16: nv_fb->r_format = 0xe800; break;
  148. case 24:
  149. case 32: nv_fb->r_format = 0xcf00; break;
  150. case 30: nv_fb->r_format = 0xd100; break;
  151. default:
  152. NV_ERROR(drm, "unknown depth %d\n", fb->depth);
  153. return -EINVAL;
  154. }
  155. if (nvbo->tile_flags & NOUVEAU_GEM_TILE_NONCONTIG) {
  156. NV_ERROR(drm, "framebuffer requires contiguous bo\n");
  157. return -EINVAL;
  158. }
  159. if (nv_device(drm->device)->chipset == 0x50)
  160. nv_fb->r_format |= (tile_flags << 8);
  161. if (!tile_flags) {
  162. if (nv_device(drm->device)->card_type < NV_D0)
  163. nv_fb->r_pitch = 0x00100000 | fb->pitches[0];
  164. else
  165. nv_fb->r_pitch = 0x01000000 | fb->pitches[0];
  166. } else {
  167. u32 mode = nvbo->tile_mode;
  168. if (nv_device(drm->device)->card_type >= NV_C0)
  169. mode >>= 4;
  170. nv_fb->r_pitch = ((fb->pitches[0] / 4) << 4) | mode;
  171. }
  172. }
  173. ret = drm_framebuffer_init(dev, fb, &nouveau_framebuffer_funcs);
  174. if (ret) {
  175. return ret;
  176. }
  177. return 0;
  178. }
  179. static struct drm_framebuffer *
  180. nouveau_user_framebuffer_create(struct drm_device *dev,
  181. struct drm_file *file_priv,
  182. struct drm_mode_fb_cmd2 *mode_cmd)
  183. {
  184. struct nouveau_framebuffer *nouveau_fb;
  185. struct drm_gem_object *gem;
  186. int ret = -ENOMEM;
  187. gem = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
  188. if (!gem)
  189. return ERR_PTR(-ENOENT);
  190. nouveau_fb = kzalloc(sizeof(struct nouveau_framebuffer), GFP_KERNEL);
  191. if (!nouveau_fb)
  192. goto err_unref;
  193. ret = nouveau_framebuffer_init(dev, nouveau_fb, mode_cmd, nouveau_gem_object(gem));
  194. if (ret)
  195. goto err;
  196. return &nouveau_fb->base;
  197. err:
  198. kfree(nouveau_fb);
  199. err_unref:
  200. drm_gem_object_unreference(gem);
  201. return ERR_PTR(ret);
  202. }
  203. static const struct drm_mode_config_funcs nouveau_mode_config_funcs = {
  204. .fb_create = nouveau_user_framebuffer_create,
  205. .output_poll_changed = nouveau_fbcon_output_poll_changed,
  206. };
  207. struct nouveau_drm_prop_enum_list {
  208. u8 gen_mask;
  209. int type;
  210. char *name;
  211. };
  212. static struct nouveau_drm_prop_enum_list underscan[] = {
  213. { 6, UNDERSCAN_AUTO, "auto" },
  214. { 6, UNDERSCAN_OFF, "off" },
  215. { 6, UNDERSCAN_ON, "on" },
  216. {}
  217. };
  218. static struct nouveau_drm_prop_enum_list dither_mode[] = {
  219. { 7, DITHERING_MODE_AUTO, "auto" },
  220. { 7, DITHERING_MODE_OFF, "off" },
  221. { 1, DITHERING_MODE_ON, "on" },
  222. { 6, DITHERING_MODE_STATIC2X2, "static 2x2" },
  223. { 6, DITHERING_MODE_DYNAMIC2X2, "dynamic 2x2" },
  224. { 4, DITHERING_MODE_TEMPORAL, "temporal" },
  225. {}
  226. };
  227. static struct nouveau_drm_prop_enum_list dither_depth[] = {
  228. { 6, DITHERING_DEPTH_AUTO, "auto" },
  229. { 6, DITHERING_DEPTH_6BPC, "6 bpc" },
  230. { 6, DITHERING_DEPTH_8BPC, "8 bpc" },
  231. {}
  232. };
  233. #define PROP_ENUM(p,gen,n,list) do { \
  234. struct nouveau_drm_prop_enum_list *l = (list); \
  235. int c = 0; \
  236. while (l->gen_mask) { \
  237. if (l->gen_mask & (1 << (gen))) \
  238. c++; \
  239. l++; \
  240. } \
  241. if (c) { \
  242. p = drm_property_create(dev, DRM_MODE_PROP_ENUM, n, c); \
  243. l = (list); \
  244. c = 0; \
  245. while (p && l->gen_mask) { \
  246. if (l->gen_mask & (1 << (gen))) { \
  247. drm_property_add_enum(p, c, l->type, l->name); \
  248. c++; \
  249. } \
  250. l++; \
  251. } \
  252. } \
  253. } while(0)
  254. int
  255. nouveau_display_init(struct drm_device *dev)
  256. {
  257. struct nouveau_display *disp = nouveau_display(dev);
  258. struct drm_connector *connector;
  259. int ret;
  260. ret = disp->init(dev);
  261. if (ret)
  262. return ret;
  263. /* enable polling for external displays */
  264. drm_kms_helper_poll_enable(dev);
  265. /* enable hotplug interrupts */
  266. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  267. struct nouveau_connector *conn = nouveau_connector(connector);
  268. if (conn->hpd_func) nouveau_event_get(conn->hpd_func);
  269. }
  270. return ret;
  271. }
  272. void
  273. nouveau_display_fini(struct drm_device *dev)
  274. {
  275. struct nouveau_display *disp = nouveau_display(dev);
  276. struct drm_connector *connector;
  277. /* disable hotplug interrupts */
  278. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  279. struct nouveau_connector *conn = nouveau_connector(connector);
  280. if (conn->hpd_func) nouveau_event_put(conn->hpd_func);
  281. }
  282. drm_kms_helper_poll_disable(dev);
  283. disp->fini(dev);
  284. }
  285. int
  286. nouveau_display_create(struct drm_device *dev)
  287. {
  288. struct nouveau_drm *drm = nouveau_drm(dev);
  289. struct nouveau_display *disp;
  290. int ret, gen;
  291. disp = drm->display = kzalloc(sizeof(*disp), GFP_KERNEL);
  292. if (!disp)
  293. return -ENOMEM;
  294. drm_mode_config_init(dev);
  295. drm_mode_create_scaling_mode_property(dev);
  296. drm_mode_create_dvi_i_properties(dev);
  297. if (nv_device(drm->device)->card_type < NV_50)
  298. gen = 0;
  299. else
  300. if (nv_device(drm->device)->card_type < NV_D0)
  301. gen = 1;
  302. else
  303. gen = 2;
  304. PROP_ENUM(disp->dithering_mode, gen, "dithering mode", dither_mode);
  305. PROP_ENUM(disp->dithering_depth, gen, "dithering depth", dither_depth);
  306. PROP_ENUM(disp->underscan_property, gen, "underscan", underscan);
  307. disp->underscan_hborder_property =
  308. drm_property_create_range(dev, 0, "underscan hborder", 0, 128);
  309. disp->underscan_vborder_property =
  310. drm_property_create_range(dev, 0, "underscan vborder", 0, 128);
  311. if (gen >= 1) {
  312. /* -90..+90 */
  313. disp->vibrant_hue_property =
  314. drm_property_create_range(dev, 0, "vibrant hue", 0, 180);
  315. /* -100..+100 */
  316. disp->color_vibrance_property =
  317. drm_property_create_range(dev, 0, "color vibrance", 0, 200);
  318. }
  319. dev->mode_config.funcs = &nouveau_mode_config_funcs;
  320. dev->mode_config.fb_base = pci_resource_start(dev->pdev, 1);
  321. dev->mode_config.min_width = 0;
  322. dev->mode_config.min_height = 0;
  323. if (nv_device(drm->device)->card_type < NV_10) {
  324. dev->mode_config.max_width = 2048;
  325. dev->mode_config.max_height = 2048;
  326. } else
  327. if (nv_device(drm->device)->card_type < NV_50) {
  328. dev->mode_config.max_width = 4096;
  329. dev->mode_config.max_height = 4096;
  330. } else {
  331. dev->mode_config.max_width = 8192;
  332. dev->mode_config.max_height = 8192;
  333. }
  334. dev->mode_config.preferred_depth = 24;
  335. dev->mode_config.prefer_shadow = 1;
  336. drm_kms_helper_poll_init(dev);
  337. drm_kms_helper_poll_disable(dev);
  338. if (drm->vbios.dcb.entries) {
  339. if (nv_device(drm->device)->card_type < NV_50)
  340. ret = nv04_display_create(dev);
  341. else
  342. ret = nv50_display_create(dev);
  343. } else {
  344. ret = 0;
  345. }
  346. if (ret)
  347. goto disp_create_err;
  348. if (dev->mode_config.num_crtc) {
  349. ret = nouveau_display_vblank_init(dev);
  350. if (ret)
  351. goto vblank_err;
  352. }
  353. nouveau_backlight_init(dev);
  354. return 0;
  355. vblank_err:
  356. disp->dtor(dev);
  357. disp_create_err:
  358. drm_kms_helper_poll_fini(dev);
  359. drm_mode_config_cleanup(dev);
  360. return ret;
  361. }
  362. void
  363. nouveau_display_destroy(struct drm_device *dev)
  364. {
  365. struct nouveau_display *disp = nouveau_display(dev);
  366. nouveau_backlight_exit(dev);
  367. nouveau_display_vblank_fini(dev);
  368. drm_kms_helper_poll_fini(dev);
  369. drm_mode_config_cleanup(dev);
  370. if (disp->dtor)
  371. disp->dtor(dev);
  372. nouveau_drm(dev)->display = NULL;
  373. kfree(disp);
  374. }
  375. int
  376. nouveau_display_suspend(struct drm_device *dev)
  377. {
  378. struct nouveau_drm *drm = nouveau_drm(dev);
  379. struct drm_crtc *crtc;
  380. nouveau_display_fini(dev);
  381. NV_INFO(drm, "unpinning framebuffer(s)...\n");
  382. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  383. struct nouveau_framebuffer *nouveau_fb;
  384. nouveau_fb = nouveau_framebuffer(crtc->fb);
  385. if (!nouveau_fb || !nouveau_fb->nvbo)
  386. continue;
  387. nouveau_bo_unpin(nouveau_fb->nvbo);
  388. }
  389. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  390. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  391. nouveau_bo_unmap(nv_crtc->cursor.nvbo);
  392. nouveau_bo_unpin(nv_crtc->cursor.nvbo);
  393. }
  394. return 0;
  395. }
  396. void
  397. nouveau_display_repin(struct drm_device *dev)
  398. {
  399. struct nouveau_drm *drm = nouveau_drm(dev);
  400. struct drm_crtc *crtc;
  401. int ret;
  402. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  403. struct nouveau_framebuffer *nouveau_fb;
  404. nouveau_fb = nouveau_framebuffer(crtc->fb);
  405. if (!nouveau_fb || !nouveau_fb->nvbo)
  406. continue;
  407. nouveau_bo_pin(nouveau_fb->nvbo, TTM_PL_FLAG_VRAM);
  408. }
  409. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  410. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  411. ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM);
  412. if (!ret)
  413. ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
  414. if (ret)
  415. NV_ERROR(drm, "Could not pin/map cursor.\n");
  416. }
  417. }
  418. void
  419. nouveau_display_resume(struct drm_device *dev)
  420. {
  421. struct drm_crtc *crtc;
  422. nouveau_display_init(dev);
  423. /* Force CLUT to get re-loaded during modeset */
  424. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  425. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  426. nv_crtc->lut.depth = 0;
  427. }
  428. drm_helper_resume_force_mode(dev);
  429. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  430. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  431. u32 offset = nv_crtc->cursor.nvbo->bo.offset;
  432. nv_crtc->cursor.set_offset(nv_crtc, offset);
  433. nv_crtc->cursor.set_pos(nv_crtc, nv_crtc->cursor_saved_x,
  434. nv_crtc->cursor_saved_y);
  435. }
  436. }
  437. static int
  438. nouveau_page_flip_emit(struct nouveau_channel *chan,
  439. struct nouveau_bo *old_bo,
  440. struct nouveau_bo *new_bo,
  441. struct nouveau_page_flip_state *s,
  442. struct nouveau_fence **pfence)
  443. {
  444. struct nouveau_fence_chan *fctx = chan->fence;
  445. struct nouveau_drm *drm = chan->drm;
  446. struct drm_device *dev = drm->dev;
  447. unsigned long flags;
  448. int ret;
  449. /* Queue it to the pending list */
  450. spin_lock_irqsave(&dev->event_lock, flags);
  451. list_add_tail(&s->head, &fctx->flip);
  452. spin_unlock_irqrestore(&dev->event_lock, flags);
  453. /* Synchronize with the old framebuffer */
  454. ret = nouveau_fence_sync(old_bo->bo.sync_obj, chan);
  455. if (ret)
  456. goto fail;
  457. /* Emit the pageflip */
  458. ret = RING_SPACE(chan, 3);
  459. if (ret)
  460. goto fail;
  461. if (nv_device(drm->device)->card_type < NV_C0) {
  462. BEGIN_NV04(chan, NvSubSw, NV_SW_PAGE_FLIP, 1);
  463. OUT_RING (chan, 0x00000000);
  464. OUT_RING (chan, 0x00000000);
  465. } else {
  466. BEGIN_NVC0(chan, 0, NV10_SUBCHAN_REF_CNT, 1);
  467. OUT_RING (chan, 0);
  468. BEGIN_IMC0(chan, 0, NVSW_SUBCHAN_PAGE_FLIP, 0x0000);
  469. }
  470. FIRE_RING (chan);
  471. ret = nouveau_fence_new(chan, false, pfence);
  472. if (ret)
  473. goto fail;
  474. return 0;
  475. fail:
  476. spin_lock_irqsave(&dev->event_lock, flags);
  477. list_del(&s->head);
  478. spin_unlock_irqrestore(&dev->event_lock, flags);
  479. return ret;
  480. }
  481. int
  482. nouveau_crtc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  483. struct drm_pending_vblank_event *event,
  484. uint32_t page_flip_flags)
  485. {
  486. struct drm_device *dev = crtc->dev;
  487. struct nouveau_drm *drm = nouveau_drm(dev);
  488. struct nouveau_bo *old_bo = nouveau_framebuffer(crtc->fb)->nvbo;
  489. struct nouveau_bo *new_bo = nouveau_framebuffer(fb)->nvbo;
  490. struct nouveau_page_flip_state *s;
  491. struct nouveau_channel *chan = drm->channel;
  492. struct nouveau_fence *fence;
  493. int ret;
  494. if (!drm->channel)
  495. return -ENODEV;
  496. s = kzalloc(sizeof(*s), GFP_KERNEL);
  497. if (!s)
  498. return -ENOMEM;
  499. /* synchronise rendering channel with the kernel's channel */
  500. spin_lock(&new_bo->bo.bdev->fence_lock);
  501. fence = nouveau_fence_ref(new_bo->bo.sync_obj);
  502. spin_unlock(&new_bo->bo.bdev->fence_lock);
  503. ret = nouveau_fence_sync(fence, chan);
  504. if (ret)
  505. return ret;
  506. if (new_bo != old_bo) {
  507. ret = nouveau_bo_pin(new_bo, TTM_PL_FLAG_VRAM);
  508. if (ret)
  509. goto fail_free;
  510. }
  511. mutex_lock(&chan->cli->mutex);
  512. ret = ttm_bo_reserve(&old_bo->bo, true, false, false, NULL);
  513. if (ret)
  514. goto fail_unpin;
  515. /* Initialize a page flip struct */
  516. *s = (struct nouveau_page_flip_state)
  517. { { }, event, nouveau_crtc(crtc)->index,
  518. fb->bits_per_pixel, fb->pitches[0], crtc->x, crtc->y,
  519. new_bo->bo.offset };
  520. /* Emit a page flip */
  521. if (nv_device(drm->device)->card_type >= NV_50) {
  522. ret = nv50_display_flip_next(crtc, fb, chan, 0);
  523. if (ret)
  524. goto fail_unreserve;
  525. } else {
  526. struct nv04_display *dispnv04 = nv04_display(dev);
  527. nouveau_bo_ref(new_bo, &dispnv04->image[nouveau_crtc(crtc)->index]);
  528. }
  529. ret = nouveau_page_flip_emit(chan, old_bo, new_bo, s, &fence);
  530. mutex_unlock(&chan->cli->mutex);
  531. if (ret)
  532. goto fail_unreserve;
  533. /* Update the crtc struct and cleanup */
  534. crtc->fb = fb;
  535. nouveau_bo_fence(old_bo, fence);
  536. ttm_bo_unreserve(&old_bo->bo);
  537. if (old_bo != new_bo)
  538. nouveau_bo_unpin(old_bo);
  539. nouveau_fence_unref(&fence);
  540. return 0;
  541. fail_unreserve:
  542. ttm_bo_unreserve(&old_bo->bo);
  543. fail_unpin:
  544. mutex_unlock(&chan->cli->mutex);
  545. if (old_bo != new_bo)
  546. nouveau_bo_unpin(new_bo);
  547. fail_free:
  548. kfree(s);
  549. return ret;
  550. }
  551. int
  552. nouveau_finish_page_flip(struct nouveau_channel *chan,
  553. struct nouveau_page_flip_state *ps)
  554. {
  555. struct nouveau_fence_chan *fctx = chan->fence;
  556. struct nouveau_drm *drm = chan->drm;
  557. struct drm_device *dev = drm->dev;
  558. struct nouveau_page_flip_state *s;
  559. unsigned long flags;
  560. spin_lock_irqsave(&dev->event_lock, flags);
  561. if (list_empty(&fctx->flip)) {
  562. NV_ERROR(drm, "unexpected pageflip\n");
  563. spin_unlock_irqrestore(&dev->event_lock, flags);
  564. return -EINVAL;
  565. }
  566. s = list_first_entry(&fctx->flip, struct nouveau_page_flip_state, head);
  567. if (s->event)
  568. drm_send_vblank_event(dev, -1, s->event);
  569. list_del(&s->head);
  570. if (ps)
  571. *ps = *s;
  572. kfree(s);
  573. spin_unlock_irqrestore(&dev->event_lock, flags);
  574. return 0;
  575. }
  576. int
  577. nouveau_flip_complete(void *data)
  578. {
  579. struct nouveau_channel *chan = data;
  580. struct nouveau_drm *drm = chan->drm;
  581. struct nouveau_page_flip_state state;
  582. if (!nouveau_finish_page_flip(chan, &state)) {
  583. if (nv_device(drm->device)->card_type < NV_50) {
  584. nv_set_crtc_base(drm->dev, state.crtc, state.offset +
  585. state.y * state.pitch +
  586. state.x * state.bpp / 8);
  587. }
  588. }
  589. return 0;
  590. }
  591. int
  592. nouveau_display_dumb_create(struct drm_file *file_priv, struct drm_device *dev,
  593. struct drm_mode_create_dumb *args)
  594. {
  595. struct nouveau_bo *bo;
  596. int ret;
  597. args->pitch = roundup(args->width * (args->bpp / 8), 256);
  598. args->size = args->pitch * args->height;
  599. args->size = roundup(args->size, PAGE_SIZE);
  600. ret = nouveau_gem_new(dev, args->size, 0, NOUVEAU_GEM_DOMAIN_VRAM, 0, 0, &bo);
  601. if (ret)
  602. return ret;
  603. ret = drm_gem_handle_create(file_priv, &bo->gem, &args->handle);
  604. drm_gem_object_unreference_unlocked(&bo->gem);
  605. return ret;
  606. }
  607. int
  608. nouveau_display_dumb_map_offset(struct drm_file *file_priv,
  609. struct drm_device *dev,
  610. uint32_t handle, uint64_t *poffset)
  611. {
  612. struct drm_gem_object *gem;
  613. gem = drm_gem_object_lookup(dev, file_priv, handle);
  614. if (gem) {
  615. struct nouveau_bo *bo = nouveau_gem_object(gem);
  616. *poffset = drm_vma_node_offset_addr(&bo->bo.vma_node);
  617. drm_gem_object_unreference_unlocked(gem);
  618. return 0;
  619. }
  620. return -ENOENT;
  621. }