nouveau_display.c 18 KB

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