nouveau_display.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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. ret = drm_framebuffer_init(dev, fb, &nouveau_framebuffer_funcs);
  71. if (ret) {
  72. return ret;
  73. }
  74. drm_helper_mode_fill_fb_struct(fb, mode_cmd);
  75. nv_fb->nvbo = nvbo;
  76. if (nv_device(drm->device)->card_type >= NV_50) {
  77. u32 tile_flags = nouveau_bo_tile_layout(nvbo);
  78. if (tile_flags == 0x7a00 ||
  79. tile_flags == 0xfe00)
  80. nv_fb->r_dma = NvEvoFB32;
  81. else
  82. if (tile_flags == 0x7000)
  83. nv_fb->r_dma = NvEvoFB16;
  84. else
  85. nv_fb->r_dma = NvEvoVRAM_LP;
  86. switch (fb->depth) {
  87. case 8: nv_fb->r_format = 0x1e00; break;
  88. case 15: nv_fb->r_format = 0xe900; break;
  89. case 16: nv_fb->r_format = 0xe800; break;
  90. case 24:
  91. case 32: nv_fb->r_format = 0xcf00; break;
  92. case 30: nv_fb->r_format = 0xd100; break;
  93. default:
  94. NV_ERROR(drm, "unknown depth %d\n", fb->depth);
  95. return -EINVAL;
  96. }
  97. if (nv_device(drm->device)->chipset == 0x50)
  98. nv_fb->r_format |= (tile_flags << 8);
  99. if (!tile_flags) {
  100. if (nv_device(drm->device)->card_type < NV_D0)
  101. nv_fb->r_pitch = 0x00100000 | fb->pitches[0];
  102. else
  103. nv_fb->r_pitch = 0x01000000 | fb->pitches[0];
  104. } else {
  105. u32 mode = nvbo->tile_mode;
  106. if (nv_device(drm->device)->card_type >= NV_C0)
  107. mode >>= 4;
  108. nv_fb->r_pitch = ((fb->pitches[0] / 4) << 4) | mode;
  109. }
  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)
  202. gpio->irq(gpio, 0, conn->hpd.func, 0xff, true);
  203. }
  204. return ret;
  205. }
  206. void
  207. nouveau_display_fini(struct drm_device *dev)
  208. {
  209. struct nouveau_drm *drm = nouveau_drm(dev);
  210. struct nouveau_display *disp = nouveau_display(dev);
  211. struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
  212. struct drm_connector *connector;
  213. /* disable hotplug interrupts */
  214. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  215. struct nouveau_connector *conn = nouveau_connector(connector);
  216. if (gpio)
  217. gpio->irq(gpio, 0, conn->hpd.func, 0xff, false);
  218. }
  219. drm_kms_helper_poll_disable(dev);
  220. disp->fini(dev);
  221. }
  222. int
  223. nouveau_display_create(struct drm_device *dev)
  224. {
  225. struct nouveau_drm *drm = nouveau_drm(dev);
  226. struct nouveau_display *disp;
  227. u32 pclass = dev->pdev->class >> 8;
  228. int ret, gen;
  229. disp = drm->display = kzalloc(sizeof(*disp), GFP_KERNEL);
  230. if (!disp)
  231. return -ENOMEM;
  232. drm_mode_config_init(dev);
  233. drm_mode_create_scaling_mode_property(dev);
  234. drm_mode_create_dvi_i_properties(dev);
  235. if (nv_device(drm->device)->card_type < NV_50)
  236. gen = 0;
  237. else
  238. if (nv_device(drm->device)->card_type < NV_D0)
  239. gen = 1;
  240. else
  241. gen = 2;
  242. PROP_ENUM(disp->dithering_mode, gen, "dithering mode", dither_mode);
  243. PROP_ENUM(disp->dithering_depth, gen, "dithering depth", dither_depth);
  244. PROP_ENUM(disp->underscan_property, gen, "underscan", underscan);
  245. disp->underscan_hborder_property =
  246. drm_property_create_range(dev, 0, "underscan hborder", 0, 128);
  247. disp->underscan_vborder_property =
  248. drm_property_create_range(dev, 0, "underscan vborder", 0, 128);
  249. if (gen >= 1) {
  250. /* -90..+90 */
  251. disp->vibrant_hue_property =
  252. drm_property_create_range(dev, 0, "vibrant hue", 0, 180);
  253. /* -100..+100 */
  254. disp->color_vibrance_property =
  255. drm_property_create_range(dev, 0, "color vibrance", 0, 200);
  256. }
  257. dev->mode_config.funcs = &nouveau_mode_config_funcs;
  258. dev->mode_config.fb_base = pci_resource_start(dev->pdev, 1);
  259. dev->mode_config.min_width = 0;
  260. dev->mode_config.min_height = 0;
  261. if (nv_device(drm->device)->card_type < NV_10) {
  262. dev->mode_config.max_width = 2048;
  263. dev->mode_config.max_height = 2048;
  264. } else
  265. if (nv_device(drm->device)->card_type < NV_50) {
  266. dev->mode_config.max_width = 4096;
  267. dev->mode_config.max_height = 4096;
  268. } else {
  269. dev->mode_config.max_width = 8192;
  270. dev->mode_config.max_height = 8192;
  271. }
  272. dev->mode_config.preferred_depth = 24;
  273. dev->mode_config.prefer_shadow = 1;
  274. drm_kms_helper_poll_init(dev);
  275. drm_kms_helper_poll_disable(dev);
  276. if (nouveau_modeset == 1 ||
  277. (nouveau_modeset < 0 && pclass == PCI_CLASS_DISPLAY_VGA)) {
  278. if (nv_device(drm->device)->card_type < NV_50)
  279. ret = nv04_display_create(dev);
  280. else
  281. ret = nv50_display_create(dev);
  282. if (ret)
  283. goto disp_create_err;
  284. if (dev->mode_config.num_crtc) {
  285. ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
  286. if (ret)
  287. goto vblank_err;
  288. }
  289. nouveau_backlight_init(dev);
  290. }
  291. return 0;
  292. vblank_err:
  293. disp->dtor(dev);
  294. disp_create_err:
  295. drm_kms_helper_poll_fini(dev);
  296. drm_mode_config_cleanup(dev);
  297. return ret;
  298. }
  299. void
  300. nouveau_display_destroy(struct drm_device *dev)
  301. {
  302. struct nouveau_display *disp = nouveau_display(dev);
  303. nouveau_backlight_exit(dev);
  304. drm_vblank_cleanup(dev);
  305. drm_kms_helper_poll_fini(dev);
  306. drm_mode_config_cleanup(dev);
  307. if (disp->dtor)
  308. disp->dtor(dev);
  309. nouveau_drm(dev)->display = NULL;
  310. kfree(disp);
  311. }
  312. int
  313. nouveau_display_suspend(struct drm_device *dev)
  314. {
  315. struct nouveau_drm *drm = nouveau_drm(dev);
  316. struct drm_crtc *crtc;
  317. nouveau_display_fini(dev);
  318. NV_INFO(drm, "unpinning framebuffer(s)...\n");
  319. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  320. struct nouveau_framebuffer *nouveau_fb;
  321. nouveau_fb = nouveau_framebuffer(crtc->fb);
  322. if (!nouveau_fb || !nouveau_fb->nvbo)
  323. continue;
  324. nouveau_bo_unpin(nouveau_fb->nvbo);
  325. }
  326. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  327. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  328. nouveau_bo_unmap(nv_crtc->cursor.nvbo);
  329. nouveau_bo_unpin(nv_crtc->cursor.nvbo);
  330. }
  331. return 0;
  332. }
  333. void
  334. nouveau_display_resume(struct drm_device *dev)
  335. {
  336. struct nouveau_drm *drm = nouveau_drm(dev);
  337. struct drm_crtc *crtc;
  338. int ret;
  339. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  340. struct nouveau_framebuffer *nouveau_fb;
  341. nouveau_fb = nouveau_framebuffer(crtc->fb);
  342. if (!nouveau_fb || !nouveau_fb->nvbo)
  343. continue;
  344. nouveau_bo_pin(nouveau_fb->nvbo, TTM_PL_FLAG_VRAM);
  345. }
  346. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  347. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  348. ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM);
  349. if (!ret)
  350. ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
  351. if (ret)
  352. NV_ERROR(drm, "Could not pin/map cursor.\n");
  353. }
  354. nouveau_fbcon_set_suspend(dev, 0);
  355. nouveau_fbcon_zfill_all(dev);
  356. nouveau_display_init(dev);
  357. /* Force CLUT to get re-loaded during modeset */
  358. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  359. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  360. nv_crtc->lut.depth = 0;
  361. }
  362. drm_helper_resume_force_mode(dev);
  363. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  364. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  365. u32 offset = nv_crtc->cursor.nvbo->bo.offset;
  366. nv_crtc->cursor.set_offset(nv_crtc, offset);
  367. nv_crtc->cursor.set_pos(nv_crtc, nv_crtc->cursor_saved_x,
  368. nv_crtc->cursor_saved_y);
  369. }
  370. }
  371. static int
  372. nouveau_page_flip_reserve(struct nouveau_bo *old_bo,
  373. struct nouveau_bo *new_bo)
  374. {
  375. int ret;
  376. ret = nouveau_bo_pin(new_bo, TTM_PL_FLAG_VRAM);
  377. if (ret)
  378. return ret;
  379. ret = ttm_bo_reserve(&new_bo->bo, false, false, false, 0);
  380. if (ret)
  381. goto fail;
  382. if (likely(old_bo != new_bo)) {
  383. ret = ttm_bo_reserve(&old_bo->bo, false, false, false, 0);
  384. if (ret)
  385. goto fail_unreserve;
  386. }
  387. return 0;
  388. fail_unreserve:
  389. ttm_bo_unreserve(&new_bo->bo);
  390. fail:
  391. nouveau_bo_unpin(new_bo);
  392. return ret;
  393. }
  394. static void
  395. nouveau_page_flip_unreserve(struct nouveau_bo *old_bo,
  396. struct nouveau_bo *new_bo,
  397. struct nouveau_fence *fence)
  398. {
  399. nouveau_bo_fence(new_bo, fence);
  400. ttm_bo_unreserve(&new_bo->bo);
  401. if (likely(old_bo != new_bo)) {
  402. nouveau_bo_fence(old_bo, fence);
  403. ttm_bo_unreserve(&old_bo->bo);
  404. }
  405. nouveau_bo_unpin(old_bo);
  406. }
  407. static int
  408. nouveau_page_flip_emit(struct nouveau_channel *chan,
  409. struct nouveau_bo *old_bo,
  410. struct nouveau_bo *new_bo,
  411. struct nouveau_page_flip_state *s,
  412. struct nouveau_fence **pfence)
  413. {
  414. struct nouveau_fence_chan *fctx = chan->fence;
  415. struct nouveau_drm *drm = chan->drm;
  416. struct drm_device *dev = drm->dev;
  417. unsigned long flags;
  418. int ret;
  419. /* Queue it to the pending list */
  420. spin_lock_irqsave(&dev->event_lock, flags);
  421. list_add_tail(&s->head, &fctx->flip);
  422. spin_unlock_irqrestore(&dev->event_lock, flags);
  423. /* Synchronize with the old framebuffer */
  424. ret = nouveau_fence_sync(old_bo->bo.sync_obj, chan);
  425. if (ret)
  426. goto fail;
  427. /* Emit the pageflip */
  428. ret = RING_SPACE(chan, 3);
  429. if (ret)
  430. goto fail;
  431. if (nv_device(drm->device)->card_type < NV_C0) {
  432. BEGIN_NV04(chan, NvSubSw, NV_SW_PAGE_FLIP, 1);
  433. OUT_RING (chan, 0x00000000);
  434. OUT_RING (chan, 0x00000000);
  435. } else {
  436. BEGIN_NVC0(chan, 0, NV10_SUBCHAN_REF_CNT, 1);
  437. OUT_RING (chan, 0);
  438. BEGIN_IMC0(chan, 0, NVSW_SUBCHAN_PAGE_FLIP, 0x0000);
  439. }
  440. FIRE_RING (chan);
  441. ret = nouveau_fence_new(chan, pfence);
  442. if (ret)
  443. goto fail;
  444. return 0;
  445. fail:
  446. spin_lock_irqsave(&dev->event_lock, flags);
  447. list_del(&s->head);
  448. spin_unlock_irqrestore(&dev->event_lock, flags);
  449. return ret;
  450. }
  451. int
  452. nouveau_crtc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  453. struct drm_pending_vblank_event *event)
  454. {
  455. struct drm_device *dev = crtc->dev;
  456. struct nouveau_drm *drm = nouveau_drm(dev);
  457. struct nouveau_bo *old_bo = nouveau_framebuffer(crtc->fb)->nvbo;
  458. struct nouveau_bo *new_bo = nouveau_framebuffer(fb)->nvbo;
  459. struct nouveau_page_flip_state *s;
  460. struct nouveau_channel *chan = NULL;
  461. struct nouveau_fence *fence;
  462. int ret;
  463. if (!drm->channel)
  464. return -ENODEV;
  465. s = kzalloc(sizeof(*s), GFP_KERNEL);
  466. if (!s)
  467. return -ENOMEM;
  468. /* Don't let the buffers go away while we flip */
  469. ret = nouveau_page_flip_reserve(old_bo, new_bo);
  470. if (ret)
  471. goto fail_free;
  472. /* Initialize a page flip struct */
  473. *s = (struct nouveau_page_flip_state)
  474. { { }, event, nouveau_crtc(crtc)->index,
  475. fb->bits_per_pixel, fb->pitches[0], crtc->x, crtc->y,
  476. new_bo->bo.offset };
  477. /* Choose the channel the flip will be handled in */
  478. fence = new_bo->bo.sync_obj;
  479. if (fence)
  480. chan = fence->channel;
  481. if (!chan)
  482. chan = drm->channel;
  483. mutex_lock(&chan->cli->mutex);
  484. /* Emit a page flip */
  485. if (nv_device(drm->device)->card_type >= NV_50) {
  486. ret = nv50_display_flip_next(crtc, fb, chan, 0);
  487. if (ret) {
  488. mutex_unlock(&chan->cli->mutex);
  489. goto fail_unreserve;
  490. }
  491. }
  492. ret = nouveau_page_flip_emit(chan, old_bo, new_bo, s, &fence);
  493. mutex_unlock(&chan->cli->mutex);
  494. if (ret)
  495. goto fail_unreserve;
  496. /* Update the crtc struct and cleanup */
  497. crtc->fb = fb;
  498. nouveau_page_flip_unreserve(old_bo, new_bo, fence);
  499. nouveau_fence_unref(&fence);
  500. return 0;
  501. fail_unreserve:
  502. nouveau_page_flip_unreserve(old_bo, new_bo, NULL);
  503. fail_free:
  504. kfree(s);
  505. return ret;
  506. }
  507. int
  508. nouveau_finish_page_flip(struct nouveau_channel *chan,
  509. struct nouveau_page_flip_state *ps)
  510. {
  511. struct nouveau_fence_chan *fctx = chan->fence;
  512. struct nouveau_drm *drm = chan->drm;
  513. struct drm_device *dev = drm->dev;
  514. struct nouveau_page_flip_state *s;
  515. unsigned long flags;
  516. spin_lock_irqsave(&dev->event_lock, flags);
  517. if (list_empty(&fctx->flip)) {
  518. NV_ERROR(drm, "unexpected pageflip\n");
  519. spin_unlock_irqrestore(&dev->event_lock, flags);
  520. return -EINVAL;
  521. }
  522. s = list_first_entry(&fctx->flip, struct nouveau_page_flip_state, head);
  523. if (s->event) {
  524. struct drm_pending_vblank_event *e = s->event;
  525. struct timeval now;
  526. do_gettimeofday(&now);
  527. e->event.sequence = 0;
  528. e->event.tv_sec = now.tv_sec;
  529. e->event.tv_usec = now.tv_usec;
  530. list_add_tail(&e->base.link, &e->base.file_priv->event_list);
  531. wake_up_interruptible(&e->base.file_priv->event_wait);
  532. }
  533. list_del(&s->head);
  534. if (ps)
  535. *ps = *s;
  536. kfree(s);
  537. spin_unlock_irqrestore(&dev->event_lock, flags);
  538. return 0;
  539. }
  540. int
  541. nouveau_flip_complete(void *data)
  542. {
  543. struct nouveau_channel *chan = data;
  544. struct nouveau_drm *drm = chan->drm;
  545. struct nouveau_page_flip_state state;
  546. if (!nouveau_finish_page_flip(chan, &state)) {
  547. if (nv_device(drm->device)->card_type < NV_50) {
  548. nv_set_crtc_base(drm->dev, state.crtc, state.offset +
  549. state.y * state.pitch +
  550. state.x * state.bpp / 8);
  551. }
  552. }
  553. return 0;
  554. }
  555. int
  556. nouveau_display_dumb_create(struct drm_file *file_priv, struct drm_device *dev,
  557. struct drm_mode_create_dumb *args)
  558. {
  559. struct nouveau_bo *bo;
  560. int ret;
  561. args->pitch = roundup(args->width * (args->bpp / 8), 256);
  562. args->size = args->pitch * args->height;
  563. args->size = roundup(args->size, PAGE_SIZE);
  564. ret = nouveau_gem_new(dev, args->size, 0, NOUVEAU_GEM_DOMAIN_VRAM, 0, 0, &bo);
  565. if (ret)
  566. return ret;
  567. ret = drm_gem_handle_create(file_priv, bo->gem, &args->handle);
  568. drm_gem_object_unreference_unlocked(bo->gem);
  569. return ret;
  570. }
  571. int
  572. nouveau_display_dumb_destroy(struct drm_file *file_priv, struct drm_device *dev,
  573. uint32_t handle)
  574. {
  575. return drm_gem_handle_delete(file_priv, handle);
  576. }
  577. int
  578. nouveau_display_dumb_map_offset(struct drm_file *file_priv,
  579. struct drm_device *dev,
  580. uint32_t handle, uint64_t *poffset)
  581. {
  582. struct drm_gem_object *gem;
  583. gem = drm_gem_object_lookup(dev, file_priv, handle);
  584. if (gem) {
  585. struct nouveau_bo *bo = gem->driver_private;
  586. *poffset = bo->bo.addr_space_offset;
  587. drm_gem_object_unreference_unlocked(gem);
  588. return 0;
  589. }
  590. return -ENOENT;
  591. }