nouveau_display.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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 (nv_device(drm->device)->chipset == 0x50)
  95. nv_fb->r_format |= (tile_flags << 8);
  96. if (!tile_flags) {
  97. if (nv_device(drm->device)->card_type < NV_D0)
  98. nv_fb->r_pitch = 0x00100000 | fb->pitches[0];
  99. else
  100. nv_fb->r_pitch = 0x01000000 | fb->pitches[0];
  101. } else {
  102. u32 mode = nvbo->tile_mode;
  103. if (nv_device(drm->device)->card_type >= NV_C0)
  104. mode >>= 4;
  105. nv_fb->r_pitch = ((fb->pitches[0] / 4) << 4) | mode;
  106. }
  107. }
  108. ret = drm_framebuffer_init(dev, fb, &nouveau_framebuffer_funcs);
  109. if (ret) {
  110. return ret;
  111. }
  112. return 0;
  113. }
  114. static struct drm_framebuffer *
  115. nouveau_user_framebuffer_create(struct drm_device *dev,
  116. struct drm_file *file_priv,
  117. struct drm_mode_fb_cmd2 *mode_cmd)
  118. {
  119. struct nouveau_framebuffer *nouveau_fb;
  120. struct drm_gem_object *gem;
  121. int ret = -ENOMEM;
  122. gem = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
  123. if (!gem)
  124. return ERR_PTR(-ENOENT);
  125. nouveau_fb = kzalloc(sizeof(struct nouveau_framebuffer), GFP_KERNEL);
  126. if (!nouveau_fb)
  127. goto err_unref;
  128. ret = nouveau_framebuffer_init(dev, nouveau_fb, mode_cmd, nouveau_gem_object(gem));
  129. if (ret)
  130. goto err;
  131. return &nouveau_fb->base;
  132. err:
  133. kfree(nouveau_fb);
  134. err_unref:
  135. drm_gem_object_unreference(gem);
  136. return ERR_PTR(ret);
  137. }
  138. static const struct drm_mode_config_funcs nouveau_mode_config_funcs = {
  139. .fb_create = nouveau_user_framebuffer_create,
  140. .output_poll_changed = nouveau_fbcon_output_poll_changed,
  141. };
  142. struct nouveau_drm_prop_enum_list {
  143. u8 gen_mask;
  144. int type;
  145. char *name;
  146. };
  147. static struct nouveau_drm_prop_enum_list underscan[] = {
  148. { 6, UNDERSCAN_AUTO, "auto" },
  149. { 6, UNDERSCAN_OFF, "off" },
  150. { 6, UNDERSCAN_ON, "on" },
  151. {}
  152. };
  153. static struct nouveau_drm_prop_enum_list dither_mode[] = {
  154. { 7, DITHERING_MODE_AUTO, "auto" },
  155. { 7, DITHERING_MODE_OFF, "off" },
  156. { 1, DITHERING_MODE_ON, "on" },
  157. { 6, DITHERING_MODE_STATIC2X2, "static 2x2" },
  158. { 6, DITHERING_MODE_DYNAMIC2X2, "dynamic 2x2" },
  159. { 4, DITHERING_MODE_TEMPORAL, "temporal" },
  160. {}
  161. };
  162. static struct nouveau_drm_prop_enum_list dither_depth[] = {
  163. { 6, DITHERING_DEPTH_AUTO, "auto" },
  164. { 6, DITHERING_DEPTH_6BPC, "6 bpc" },
  165. { 6, DITHERING_DEPTH_8BPC, "8 bpc" },
  166. {}
  167. };
  168. #define PROP_ENUM(p,gen,n,list) do { \
  169. struct nouveau_drm_prop_enum_list *l = (list); \
  170. int c = 0; \
  171. while (l->gen_mask) { \
  172. if (l->gen_mask & (1 << (gen))) \
  173. c++; \
  174. l++; \
  175. } \
  176. if (c) { \
  177. p = drm_property_create(dev, DRM_MODE_PROP_ENUM, n, c); \
  178. l = (list); \
  179. c = 0; \
  180. while (p && l->gen_mask) { \
  181. if (l->gen_mask & (1 << (gen))) { \
  182. drm_property_add_enum(p, c, l->type, l->name); \
  183. c++; \
  184. } \
  185. l++; \
  186. } \
  187. } \
  188. } while(0)
  189. int
  190. nouveau_display_init(struct drm_device *dev)
  191. {
  192. struct nouveau_drm *drm = nouveau_drm(dev);
  193. struct nouveau_display *disp = nouveau_display(dev);
  194. struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
  195. struct drm_connector *connector;
  196. int ret;
  197. ret = disp->init(dev);
  198. if (ret)
  199. return ret;
  200. /* enable polling for external displays */
  201. drm_kms_helper_poll_enable(dev);
  202. /* enable hotplug interrupts */
  203. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  204. struct nouveau_connector *conn = nouveau_connector(connector);
  205. if (gpio && conn->hpd.func != DCB_GPIO_UNUSED) {
  206. nouveau_event_get(gpio->events, conn->hpd.line,
  207. &conn->hpd_func);
  208. }
  209. }
  210. return ret;
  211. }
  212. void
  213. nouveau_display_fini(struct drm_device *dev)
  214. {
  215. struct nouveau_drm *drm = nouveau_drm(dev);
  216. struct nouveau_display *disp = nouveau_display(dev);
  217. struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
  218. struct drm_connector *connector;
  219. /* disable hotplug interrupts */
  220. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  221. struct nouveau_connector *conn = nouveau_connector(connector);
  222. if (gpio && conn->hpd.func != DCB_GPIO_UNUSED) {
  223. nouveau_event_put(gpio->events, conn->hpd.line,
  224. &conn->hpd_func);
  225. }
  226. }
  227. drm_kms_helper_poll_disable(dev);
  228. disp->fini(dev);
  229. }
  230. int
  231. nouveau_display_create(struct drm_device *dev)
  232. {
  233. struct nouveau_drm *drm = nouveau_drm(dev);
  234. struct nouveau_display *disp;
  235. u32 pclass = dev->pdev->class >> 8;
  236. int ret, gen;
  237. disp = drm->display = kzalloc(sizeof(*disp), GFP_KERNEL);
  238. if (!disp)
  239. return -ENOMEM;
  240. drm_mode_config_init(dev);
  241. drm_mode_create_scaling_mode_property(dev);
  242. drm_mode_create_dvi_i_properties(dev);
  243. if (nv_device(drm->device)->card_type < NV_50)
  244. gen = 0;
  245. else
  246. if (nv_device(drm->device)->card_type < NV_D0)
  247. gen = 1;
  248. else
  249. gen = 2;
  250. PROP_ENUM(disp->dithering_mode, gen, "dithering mode", dither_mode);
  251. PROP_ENUM(disp->dithering_depth, gen, "dithering depth", dither_depth);
  252. PROP_ENUM(disp->underscan_property, gen, "underscan", underscan);
  253. disp->underscan_hborder_property =
  254. drm_property_create_range(dev, 0, "underscan hborder", 0, 128);
  255. disp->underscan_vborder_property =
  256. drm_property_create_range(dev, 0, "underscan vborder", 0, 128);
  257. if (gen >= 1) {
  258. /* -90..+90 */
  259. disp->vibrant_hue_property =
  260. drm_property_create_range(dev, 0, "vibrant hue", 0, 180);
  261. /* -100..+100 */
  262. disp->color_vibrance_property =
  263. drm_property_create_range(dev, 0, "color vibrance", 0, 200);
  264. }
  265. dev->mode_config.funcs = &nouveau_mode_config_funcs;
  266. dev->mode_config.fb_base = pci_resource_start(dev->pdev, 1);
  267. dev->mode_config.min_width = 0;
  268. dev->mode_config.min_height = 0;
  269. if (nv_device(drm->device)->card_type < NV_10) {
  270. dev->mode_config.max_width = 2048;
  271. dev->mode_config.max_height = 2048;
  272. } else
  273. if (nv_device(drm->device)->card_type < NV_50) {
  274. dev->mode_config.max_width = 4096;
  275. dev->mode_config.max_height = 4096;
  276. } else {
  277. dev->mode_config.max_width = 8192;
  278. dev->mode_config.max_height = 8192;
  279. }
  280. dev->mode_config.preferred_depth = 24;
  281. dev->mode_config.prefer_shadow = 1;
  282. drm_kms_helper_poll_init(dev);
  283. drm_kms_helper_poll_disable(dev);
  284. if (nouveau_modeset == 1 ||
  285. (nouveau_modeset < 0 && pclass == PCI_CLASS_DISPLAY_VGA)) {
  286. if (drm->vbios.dcb.entries) {
  287. if (nv_device(drm->device)->card_type < NV_50)
  288. ret = nv04_display_create(dev);
  289. else
  290. ret = nv50_display_create(dev);
  291. } else {
  292. ret = 0;
  293. }
  294. if (ret)
  295. goto disp_create_err;
  296. if (dev->mode_config.num_crtc) {
  297. ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
  298. if (ret)
  299. goto vblank_err;
  300. }
  301. nouveau_backlight_init(dev);
  302. }
  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_INFO(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_resume(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. nouveau_fbcon_set_suspend(dev, 0);
  367. nouveau_fbcon_zfill_all(dev);
  368. nouveau_display_init(dev);
  369. /* Force CLUT to get re-loaded during modeset */
  370. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  371. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  372. nv_crtc->lut.depth = 0;
  373. }
  374. drm_helper_resume_force_mode(dev);
  375. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  376. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  377. u32 offset = nv_crtc->cursor.nvbo->bo.offset;
  378. nv_crtc->cursor.set_offset(nv_crtc, offset);
  379. nv_crtc->cursor.set_pos(nv_crtc, nv_crtc->cursor_saved_x,
  380. nv_crtc->cursor_saved_y);
  381. }
  382. }
  383. static int
  384. nouveau_page_flip_emit(struct nouveau_channel *chan,
  385. struct nouveau_bo *old_bo,
  386. struct nouveau_bo *new_bo,
  387. struct nouveau_page_flip_state *s,
  388. struct nouveau_fence **pfence)
  389. {
  390. struct nouveau_fence_chan *fctx = chan->fence;
  391. struct nouveau_drm *drm = chan->drm;
  392. struct drm_device *dev = drm->dev;
  393. unsigned long flags;
  394. int ret;
  395. /* Queue it to the pending list */
  396. spin_lock_irqsave(&dev->event_lock, flags);
  397. list_add_tail(&s->head, &fctx->flip);
  398. spin_unlock_irqrestore(&dev->event_lock, flags);
  399. /* Synchronize with the old framebuffer */
  400. ret = nouveau_fence_sync(old_bo->bo.sync_obj, chan);
  401. if (ret)
  402. goto fail;
  403. /* Emit the pageflip */
  404. ret = RING_SPACE(chan, 3);
  405. if (ret)
  406. goto fail;
  407. if (nv_device(drm->device)->card_type < NV_C0) {
  408. BEGIN_NV04(chan, NvSubSw, NV_SW_PAGE_FLIP, 1);
  409. OUT_RING (chan, 0x00000000);
  410. OUT_RING (chan, 0x00000000);
  411. } else {
  412. BEGIN_NVC0(chan, 0, NV10_SUBCHAN_REF_CNT, 1);
  413. OUT_RING (chan, 0);
  414. BEGIN_IMC0(chan, 0, NVSW_SUBCHAN_PAGE_FLIP, 0x0000);
  415. }
  416. FIRE_RING (chan);
  417. ret = nouveau_fence_new(chan, false, pfence);
  418. if (ret)
  419. goto fail;
  420. return 0;
  421. fail:
  422. spin_lock_irqsave(&dev->event_lock, flags);
  423. list_del(&s->head);
  424. spin_unlock_irqrestore(&dev->event_lock, flags);
  425. return ret;
  426. }
  427. int
  428. nouveau_crtc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  429. struct drm_pending_vblank_event *event)
  430. {
  431. struct drm_device *dev = crtc->dev;
  432. struct nouveau_drm *drm = nouveau_drm(dev);
  433. struct nouveau_bo *old_bo = nouveau_framebuffer(crtc->fb)->nvbo;
  434. struct nouveau_bo *new_bo = nouveau_framebuffer(fb)->nvbo;
  435. struct nouveau_page_flip_state *s;
  436. struct nouveau_channel *chan = NULL;
  437. struct nouveau_fence *fence;
  438. struct ttm_validate_buffer resv[2] = {
  439. { .bo = &old_bo->bo },
  440. { .bo = &new_bo->bo },
  441. };
  442. struct ww_acquire_ctx ticket;
  443. LIST_HEAD(res);
  444. int ret;
  445. if (!drm->channel)
  446. return -ENODEV;
  447. s = kzalloc(sizeof(*s), GFP_KERNEL);
  448. if (!s)
  449. return -ENOMEM;
  450. /* Choose the channel the flip will be handled in */
  451. spin_lock(&old_bo->bo.bdev->fence_lock);
  452. fence = new_bo->bo.sync_obj;
  453. if (fence)
  454. chan = fence->channel;
  455. if (!chan)
  456. chan = drm->channel;
  457. spin_unlock(&old_bo->bo.bdev->fence_lock);
  458. if (new_bo != old_bo) {
  459. ret = nouveau_bo_pin(new_bo, TTM_PL_FLAG_VRAM);
  460. if (ret)
  461. goto fail_free;
  462. list_add(&resv[1].head, &res);
  463. }
  464. list_add(&resv[0].head, &res);
  465. mutex_lock(&chan->cli->mutex);
  466. ret = ttm_eu_reserve_buffers(&ticket, &res);
  467. if (ret)
  468. goto fail_unpin;
  469. /* Initialize a page flip struct */
  470. *s = (struct nouveau_page_flip_state)
  471. { { }, event, nouveau_crtc(crtc)->index,
  472. fb->bits_per_pixel, fb->pitches[0], crtc->x, crtc->y,
  473. new_bo->bo.offset };
  474. /* Emit a page flip */
  475. if (nv_device(drm->device)->card_type >= NV_50) {
  476. ret = nv50_display_flip_next(crtc, fb, chan, 0);
  477. if (ret)
  478. goto fail_unreserve;
  479. }
  480. ret = nouveau_page_flip_emit(chan, old_bo, new_bo, s, &fence);
  481. mutex_unlock(&chan->cli->mutex);
  482. if (ret)
  483. goto fail_unreserve;
  484. /* Update the crtc struct and cleanup */
  485. crtc->fb = fb;
  486. ttm_eu_fence_buffer_objects(&ticket, &res, fence);
  487. if (old_bo != new_bo)
  488. nouveau_bo_unpin(old_bo);
  489. nouveau_fence_unref(&fence);
  490. return 0;
  491. fail_unreserve:
  492. ttm_eu_backoff_reservation(&ticket, &res);
  493. fail_unpin:
  494. mutex_unlock(&chan->cli->mutex);
  495. if (old_bo != new_bo)
  496. nouveau_bo_unpin(new_bo);
  497. fail_free:
  498. kfree(s);
  499. return ret;
  500. }
  501. int
  502. nouveau_finish_page_flip(struct nouveau_channel *chan,
  503. struct nouveau_page_flip_state *ps)
  504. {
  505. struct nouveau_fence_chan *fctx = chan->fence;
  506. struct nouveau_drm *drm = chan->drm;
  507. struct drm_device *dev = drm->dev;
  508. struct nouveau_page_flip_state *s;
  509. unsigned long flags;
  510. spin_lock_irqsave(&dev->event_lock, flags);
  511. if (list_empty(&fctx->flip)) {
  512. NV_ERROR(drm, "unexpected pageflip\n");
  513. spin_unlock_irqrestore(&dev->event_lock, flags);
  514. return -EINVAL;
  515. }
  516. s = list_first_entry(&fctx->flip, struct nouveau_page_flip_state, head);
  517. if (s->event)
  518. drm_send_vblank_event(dev, -1, s->event);
  519. list_del(&s->head);
  520. if (ps)
  521. *ps = *s;
  522. kfree(s);
  523. spin_unlock_irqrestore(&dev->event_lock, flags);
  524. return 0;
  525. }
  526. int
  527. nouveau_flip_complete(void *data)
  528. {
  529. struct nouveau_channel *chan = data;
  530. struct nouveau_drm *drm = chan->drm;
  531. struct nouveau_page_flip_state state;
  532. if (!nouveau_finish_page_flip(chan, &state)) {
  533. if (nv_device(drm->device)->card_type < NV_50) {
  534. nv_set_crtc_base(drm->dev, state.crtc, state.offset +
  535. state.y * state.pitch +
  536. state.x * state.bpp / 8);
  537. }
  538. }
  539. return 0;
  540. }
  541. int
  542. nouveau_display_dumb_create(struct drm_file *file_priv, struct drm_device *dev,
  543. struct drm_mode_create_dumb *args)
  544. {
  545. struct nouveau_bo *bo;
  546. int ret;
  547. args->pitch = roundup(args->width * (args->bpp / 8), 256);
  548. args->size = args->pitch * args->height;
  549. args->size = roundup(args->size, PAGE_SIZE);
  550. ret = nouveau_gem_new(dev, args->size, 0, NOUVEAU_GEM_DOMAIN_VRAM, 0, 0, &bo);
  551. if (ret)
  552. return ret;
  553. ret = drm_gem_handle_create(file_priv, bo->gem, &args->handle);
  554. drm_gem_object_unreference_unlocked(bo->gem);
  555. return ret;
  556. }
  557. int
  558. nouveau_display_dumb_destroy(struct drm_file *file_priv, struct drm_device *dev,
  559. uint32_t handle)
  560. {
  561. return drm_gem_handle_delete(file_priv, handle);
  562. }
  563. int
  564. nouveau_display_dumb_map_offset(struct drm_file *file_priv,
  565. struct drm_device *dev,
  566. uint32_t handle, uint64_t *poffset)
  567. {
  568. struct drm_gem_object *gem;
  569. gem = drm_gem_object_lookup(dev, file_priv, handle);
  570. if (gem) {
  571. struct nouveau_bo *bo = gem->driver_private;
  572. *poffset = bo->bo.addr_space_offset;
  573. drm_gem_object_unreference_unlocked(gem);
  574. return 0;
  575. }
  576. return -ENOENT;
  577. }