nouveau_display.c 19 KB

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