nouveau_display.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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. if (nv_device(drm->device)->chipset < 0x11)
  337. dev->mode_config.async_page_flip = false;
  338. else
  339. dev->mode_config.async_page_flip = true;
  340. drm_kms_helper_poll_init(dev);
  341. drm_kms_helper_poll_disable(dev);
  342. if (drm->vbios.dcb.entries) {
  343. if (nv_device(drm->device)->card_type < NV_50)
  344. ret = nv04_display_create(dev);
  345. else
  346. ret = nv50_display_create(dev);
  347. } else {
  348. ret = 0;
  349. }
  350. if (ret)
  351. goto disp_create_err;
  352. if (dev->mode_config.num_crtc) {
  353. ret = nouveau_display_vblank_init(dev);
  354. if (ret)
  355. goto vblank_err;
  356. }
  357. nouveau_backlight_init(dev);
  358. return 0;
  359. vblank_err:
  360. disp->dtor(dev);
  361. disp_create_err:
  362. drm_kms_helper_poll_fini(dev);
  363. drm_mode_config_cleanup(dev);
  364. return ret;
  365. }
  366. void
  367. nouveau_display_destroy(struct drm_device *dev)
  368. {
  369. struct nouveau_display *disp = nouveau_display(dev);
  370. nouveau_backlight_exit(dev);
  371. nouveau_display_vblank_fini(dev);
  372. drm_kms_helper_poll_fini(dev);
  373. drm_mode_config_cleanup(dev);
  374. if (disp->dtor)
  375. disp->dtor(dev);
  376. nouveau_drm(dev)->display = NULL;
  377. kfree(disp);
  378. }
  379. int
  380. nouveau_display_suspend(struct drm_device *dev)
  381. {
  382. struct nouveau_drm *drm = nouveau_drm(dev);
  383. struct drm_crtc *crtc;
  384. nouveau_display_fini(dev);
  385. NV_INFO(drm, "unpinning framebuffer(s)...\n");
  386. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  387. struct nouveau_framebuffer *nouveau_fb;
  388. nouveau_fb = nouveau_framebuffer(crtc->fb);
  389. if (!nouveau_fb || !nouveau_fb->nvbo)
  390. continue;
  391. nouveau_bo_unpin(nouveau_fb->nvbo);
  392. }
  393. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  394. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  395. nouveau_bo_unmap(nv_crtc->cursor.nvbo);
  396. nouveau_bo_unpin(nv_crtc->cursor.nvbo);
  397. }
  398. return 0;
  399. }
  400. void
  401. nouveau_display_repin(struct drm_device *dev)
  402. {
  403. struct nouveau_drm *drm = nouveau_drm(dev);
  404. struct drm_crtc *crtc;
  405. int ret;
  406. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  407. struct nouveau_framebuffer *nouveau_fb;
  408. nouveau_fb = nouveau_framebuffer(crtc->fb);
  409. if (!nouveau_fb || !nouveau_fb->nvbo)
  410. continue;
  411. nouveau_bo_pin(nouveau_fb->nvbo, TTM_PL_FLAG_VRAM);
  412. }
  413. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  414. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  415. ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM);
  416. if (!ret)
  417. ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
  418. if (ret)
  419. NV_ERROR(drm, "Could not pin/map cursor.\n");
  420. }
  421. }
  422. void
  423. nouveau_display_resume(struct drm_device *dev)
  424. {
  425. struct drm_crtc *crtc;
  426. nouveau_display_init(dev);
  427. /* Force CLUT to get re-loaded during modeset */
  428. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  429. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  430. nv_crtc->lut.depth = 0;
  431. }
  432. drm_helper_resume_force_mode(dev);
  433. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  434. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  435. u32 offset = nv_crtc->cursor.nvbo->bo.offset;
  436. nv_crtc->cursor.set_offset(nv_crtc, offset);
  437. nv_crtc->cursor.set_pos(nv_crtc, nv_crtc->cursor_saved_x,
  438. nv_crtc->cursor_saved_y);
  439. }
  440. }
  441. static int
  442. nouveau_page_flip_emit(struct nouveau_channel *chan,
  443. struct nouveau_bo *old_bo,
  444. struct nouveau_bo *new_bo,
  445. struct nouveau_page_flip_state *s,
  446. struct nouveau_fence **pfence)
  447. {
  448. struct nouveau_fence_chan *fctx = chan->fence;
  449. struct nouveau_drm *drm = chan->drm;
  450. struct drm_device *dev = drm->dev;
  451. unsigned long flags;
  452. int ret;
  453. /* Queue it to the pending list */
  454. spin_lock_irqsave(&dev->event_lock, flags);
  455. list_add_tail(&s->head, &fctx->flip);
  456. spin_unlock_irqrestore(&dev->event_lock, flags);
  457. /* Synchronize with the old framebuffer */
  458. ret = nouveau_fence_sync(old_bo->bo.sync_obj, chan);
  459. if (ret)
  460. goto fail;
  461. /* Emit the pageflip */
  462. ret = RING_SPACE(chan, 2);
  463. if (ret)
  464. goto fail;
  465. if (nv_device(drm->device)->card_type < NV_C0)
  466. BEGIN_NV04(chan, NvSubSw, NV_SW_PAGE_FLIP, 1);
  467. else
  468. BEGIN_NVC0(chan, FermiSw, NV_SW_PAGE_FLIP, 1);
  469. OUT_RING (chan, 0x00000000);
  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, u32 flags)
  484. {
  485. const int swap_interval = (flags & DRM_MODE_PAGE_FLIP_ASYNC) ? 0 : 1;
  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, swap_interval);
  523. if (ret)
  524. goto fail_unreserve;
  525. } else {
  526. struct nv04_display *dispnv04 = nv04_display(dev);
  527. int head = nouveau_crtc(crtc)->index;
  528. if (swap_interval) {
  529. ret = RING_SPACE(chan, 8);
  530. if (ret)
  531. goto fail_unreserve;
  532. BEGIN_NV04(chan, NvSubImageBlit, 0x012c, 1);
  533. OUT_RING (chan, 0);
  534. BEGIN_NV04(chan, NvSubImageBlit, 0x0134, 1);
  535. OUT_RING (chan, head);
  536. BEGIN_NV04(chan, NvSubImageBlit, 0x0100, 1);
  537. OUT_RING (chan, 0);
  538. BEGIN_NV04(chan, NvSubImageBlit, 0x0130, 1);
  539. OUT_RING (chan, 0);
  540. }
  541. nouveau_bo_ref(new_bo, &dispnv04->image[head]);
  542. }
  543. ret = nouveau_page_flip_emit(chan, old_bo, new_bo, s, &fence);
  544. mutex_unlock(&chan->cli->mutex);
  545. if (ret)
  546. goto fail_unreserve;
  547. /* Update the crtc struct and cleanup */
  548. crtc->fb = fb;
  549. nouveau_bo_fence(old_bo, fence);
  550. ttm_bo_unreserve(&old_bo->bo);
  551. if (old_bo != new_bo)
  552. nouveau_bo_unpin(old_bo);
  553. nouveau_fence_unref(&fence);
  554. return 0;
  555. fail_unreserve:
  556. ttm_bo_unreserve(&old_bo->bo);
  557. fail_unpin:
  558. mutex_unlock(&chan->cli->mutex);
  559. if (old_bo != new_bo)
  560. nouveau_bo_unpin(new_bo);
  561. fail_free:
  562. kfree(s);
  563. return ret;
  564. }
  565. int
  566. nouveau_finish_page_flip(struct nouveau_channel *chan,
  567. struct nouveau_page_flip_state *ps)
  568. {
  569. struct nouveau_fence_chan *fctx = chan->fence;
  570. struct nouveau_drm *drm = chan->drm;
  571. struct drm_device *dev = drm->dev;
  572. struct nouveau_page_flip_state *s;
  573. unsigned long flags;
  574. spin_lock_irqsave(&dev->event_lock, flags);
  575. if (list_empty(&fctx->flip)) {
  576. NV_ERROR(drm, "unexpected pageflip\n");
  577. spin_unlock_irqrestore(&dev->event_lock, flags);
  578. return -EINVAL;
  579. }
  580. s = list_first_entry(&fctx->flip, struct nouveau_page_flip_state, head);
  581. if (s->event)
  582. drm_send_vblank_event(dev, -1, s->event);
  583. list_del(&s->head);
  584. if (ps)
  585. *ps = *s;
  586. kfree(s);
  587. spin_unlock_irqrestore(&dev->event_lock, flags);
  588. return 0;
  589. }
  590. int
  591. nouveau_flip_complete(void *data)
  592. {
  593. struct nouveau_channel *chan = data;
  594. struct nouveau_drm *drm = chan->drm;
  595. struct nouveau_page_flip_state state;
  596. if (!nouveau_finish_page_flip(chan, &state)) {
  597. if (nv_device(drm->device)->card_type < NV_50) {
  598. nv_set_crtc_base(drm->dev, state.crtc, state.offset +
  599. state.y * state.pitch +
  600. state.x * state.bpp / 8);
  601. }
  602. }
  603. return 0;
  604. }
  605. int
  606. nouveau_display_dumb_create(struct drm_file *file_priv, struct drm_device *dev,
  607. struct drm_mode_create_dumb *args)
  608. {
  609. struct nouveau_bo *bo;
  610. int ret;
  611. args->pitch = roundup(args->width * (args->bpp / 8), 256);
  612. args->size = args->pitch * args->height;
  613. args->size = roundup(args->size, PAGE_SIZE);
  614. ret = nouveau_gem_new(dev, args->size, 0, NOUVEAU_GEM_DOMAIN_VRAM, 0, 0, &bo);
  615. if (ret)
  616. return ret;
  617. ret = drm_gem_handle_create(file_priv, &bo->gem, &args->handle);
  618. drm_gem_object_unreference_unlocked(&bo->gem);
  619. return ret;
  620. }
  621. int
  622. nouveau_display_dumb_map_offset(struct drm_file *file_priv,
  623. struct drm_device *dev,
  624. uint32_t handle, uint64_t *poffset)
  625. {
  626. struct drm_gem_object *gem;
  627. gem = drm_gem_object_lookup(dev, file_priv, handle);
  628. if (gem) {
  629. struct nouveau_bo *bo = nouveau_gem_object(gem);
  630. *poffset = drm_vma_node_offset_addr(&bo->bo.vma_node);
  631. drm_gem_object_unreference_unlocked(gem);
  632. return 0;
  633. }
  634. return -ENOENT;
  635. }