nouveau_display.c 19 KB

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