nouveau_display.c 20 KB

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