nouveau_display.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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 "drmP.h"
  27. #include "drm_crtc_helper.h"
  28. #include "nouveau_drv.h"
  29. #include "nouveau_fb.h"
  30. #include "nouveau_fbcon.h"
  31. #include "nouveau_hw.h"
  32. #include "nouveau_crtc.h"
  33. #include "nouveau_dma.h"
  34. #include "nouveau_connector.h"
  35. #include "nouveau_gpio.h"
  36. #include "nv50_display.h"
  37. static void
  38. nouveau_user_framebuffer_destroy(struct drm_framebuffer *drm_fb)
  39. {
  40. struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
  41. if (fb->nvbo)
  42. drm_gem_object_unreference_unlocked(fb->nvbo->gem);
  43. drm_framebuffer_cleanup(drm_fb);
  44. kfree(fb);
  45. }
  46. static int
  47. nouveau_user_framebuffer_create_handle(struct drm_framebuffer *drm_fb,
  48. struct drm_file *file_priv,
  49. unsigned int *handle)
  50. {
  51. struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
  52. return drm_gem_handle_create(file_priv, fb->nvbo->gem, handle);
  53. }
  54. static const struct drm_framebuffer_funcs nouveau_framebuffer_funcs = {
  55. .destroy = nouveau_user_framebuffer_destroy,
  56. .create_handle = nouveau_user_framebuffer_create_handle,
  57. };
  58. int
  59. nouveau_framebuffer_init(struct drm_device *dev,
  60. struct nouveau_framebuffer *nv_fb,
  61. struct drm_mode_fb_cmd2 *mode_cmd,
  62. struct nouveau_bo *nvbo)
  63. {
  64. struct drm_nouveau_private *dev_priv = dev->dev_private;
  65. struct drm_framebuffer *fb = &nv_fb->base;
  66. int ret;
  67. ret = drm_framebuffer_init(dev, fb, &nouveau_framebuffer_funcs);
  68. if (ret) {
  69. return ret;
  70. }
  71. drm_helper_mode_fill_fb_struct(fb, mode_cmd);
  72. nv_fb->nvbo = nvbo;
  73. if (dev_priv->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 = NV50_EVO_CRTC_FB_DEPTH_8; break;
  85. case 15: nv_fb->r_format = NV50_EVO_CRTC_FB_DEPTH_15; break;
  86. case 16: nv_fb->r_format = NV50_EVO_CRTC_FB_DEPTH_16; break;
  87. case 24:
  88. case 32: nv_fb->r_format = NV50_EVO_CRTC_FB_DEPTH_24; break;
  89. case 30: nv_fb->r_format = NV50_EVO_CRTC_FB_DEPTH_30; break;
  90. default:
  91. NV_ERROR(dev, "unknown depth %d\n", fb->depth);
  92. return -EINVAL;
  93. }
  94. if (dev_priv->chipset == 0x50)
  95. nv_fb->r_format |= (tile_flags << 8);
  96. if (!tile_flags) {
  97. if (dev_priv->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 (dev_priv->card_type >= NV_C0)
  104. mode >>= 4;
  105. nv_fb->r_pitch = ((fb->pitches[0] / 4) << 4) | mode;
  106. }
  107. }
  108. return 0;
  109. }
  110. static struct drm_framebuffer *
  111. nouveau_user_framebuffer_create(struct drm_device *dev,
  112. struct drm_file *file_priv,
  113. struct drm_mode_fb_cmd2 *mode_cmd)
  114. {
  115. struct nouveau_framebuffer *nouveau_fb;
  116. struct drm_gem_object *gem;
  117. int ret;
  118. gem = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
  119. if (!gem)
  120. return ERR_PTR(-ENOENT);
  121. nouveau_fb = kzalloc(sizeof(struct nouveau_framebuffer), GFP_KERNEL);
  122. if (!nouveau_fb)
  123. return ERR_PTR(-ENOMEM);
  124. ret = nouveau_framebuffer_init(dev, nouveau_fb, mode_cmd, nouveau_gem_object(gem));
  125. if (ret) {
  126. drm_gem_object_unreference(gem);
  127. return ERR_PTR(ret);
  128. }
  129. return &nouveau_fb->base;
  130. }
  131. static const struct drm_mode_config_funcs nouveau_mode_config_funcs = {
  132. .fb_create = nouveau_user_framebuffer_create,
  133. .output_poll_changed = nouveau_fbcon_output_poll_changed,
  134. };
  135. struct drm_prop_enum_list {
  136. u8 gen_mask;
  137. int type;
  138. char *name;
  139. };
  140. static struct drm_prop_enum_list underscan[] = {
  141. { 6, UNDERSCAN_AUTO, "auto" },
  142. { 6, UNDERSCAN_OFF, "off" },
  143. { 6, UNDERSCAN_ON, "on" },
  144. {}
  145. };
  146. static struct drm_prop_enum_list dither_mode[] = {
  147. { 7, DITHERING_MODE_AUTO, "auto" },
  148. { 7, DITHERING_MODE_OFF, "off" },
  149. { 1, DITHERING_MODE_ON, "on" },
  150. { 6, DITHERING_MODE_STATIC2X2, "static 2x2" },
  151. { 6, DITHERING_MODE_DYNAMIC2X2, "dynamic 2x2" },
  152. { 4, DITHERING_MODE_TEMPORAL, "temporal" },
  153. {}
  154. };
  155. static struct drm_prop_enum_list dither_depth[] = {
  156. { 6, DITHERING_DEPTH_AUTO, "auto" },
  157. { 6, DITHERING_DEPTH_6BPC, "6 bpc" },
  158. { 6, DITHERING_DEPTH_8BPC, "8 bpc" },
  159. {}
  160. };
  161. #define PROP_ENUM(p,gen,n,list) do { \
  162. struct drm_prop_enum_list *l = (list); \
  163. int c = 0; \
  164. while (l->gen_mask) { \
  165. if (l->gen_mask & (1 << (gen))) \
  166. c++; \
  167. l++; \
  168. } \
  169. if (c) { \
  170. p = drm_property_create(dev, DRM_MODE_PROP_ENUM, n, c); \
  171. l = (list); \
  172. c = 0; \
  173. while (p && l->gen_mask) { \
  174. if (l->gen_mask & (1 << (gen))) { \
  175. drm_property_add_enum(p, c, l->type, l->name); \
  176. c++; \
  177. } \
  178. l++; \
  179. } \
  180. } \
  181. } while(0)
  182. int
  183. nouveau_display_init(struct drm_device *dev)
  184. {
  185. struct drm_nouveau_private *dev_priv = dev->dev_private;
  186. struct nouveau_display_engine *disp = &dev_priv->engine.display;
  187. struct drm_connector *connector;
  188. int ret;
  189. ret = disp->init(dev);
  190. if (ret)
  191. return ret;
  192. /* power on internal panel if it's not already. the init tables of
  193. * some vbios default this to off for some reason, causing the
  194. * panel to not work after resume
  195. */
  196. if (nouveau_gpio_func_get(dev, DCB_GPIO_PANEL_POWER) == 0) {
  197. nouveau_gpio_func_set(dev, DCB_GPIO_PANEL_POWER, true);
  198. msleep(300);
  199. }
  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. nouveau_gpio_irq(dev, 0, conn->hpd, 0xff, true);
  206. }
  207. return ret;
  208. }
  209. void
  210. nouveau_display_fini(struct drm_device *dev)
  211. {
  212. struct drm_nouveau_private *dev_priv = dev->dev_private;
  213. struct nouveau_display_engine *disp = &dev_priv->engine.display;
  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. nouveau_gpio_irq(dev, 0, conn->hpd, 0xff, false);
  219. }
  220. drm_kms_helper_poll_disable(dev);
  221. disp->fini(dev);
  222. }
  223. int
  224. nouveau_display_create(struct drm_device *dev)
  225. {
  226. struct drm_nouveau_private *dev_priv = dev->dev_private;
  227. struct nouveau_display_engine *disp = &dev_priv->engine.display;
  228. int ret, gen;
  229. drm_mode_config_init(dev);
  230. drm_mode_create_scaling_mode_property(dev);
  231. drm_mode_create_dvi_i_properties(dev);
  232. if (dev_priv->card_type < NV_50)
  233. gen = 0;
  234. else
  235. if (dev_priv->card_type < NV_D0)
  236. gen = 1;
  237. else
  238. gen = 2;
  239. PROP_ENUM(disp->dithering_mode, gen, "dithering mode", dither_mode);
  240. PROP_ENUM(disp->dithering_depth, gen, "dithering depth", dither_depth);
  241. PROP_ENUM(disp->underscan_property, gen, "underscan", underscan);
  242. disp->underscan_hborder_property =
  243. drm_property_create(dev, DRM_MODE_PROP_RANGE,
  244. "underscan hborder", 2);
  245. disp->underscan_hborder_property->values[0] = 0;
  246. disp->underscan_hborder_property->values[1] = 128;
  247. disp->underscan_vborder_property =
  248. drm_property_create(dev, DRM_MODE_PROP_RANGE,
  249. "underscan vborder", 2);
  250. disp->underscan_vborder_property->values[0] = 0;
  251. disp->underscan_vborder_property->values[1] = 128;
  252. dev->mode_config.funcs = (void *)&nouveau_mode_config_funcs;
  253. dev->mode_config.fb_base = pci_resource_start(dev->pdev, 1);
  254. dev->mode_config.min_width = 0;
  255. dev->mode_config.min_height = 0;
  256. if (dev_priv->card_type < NV_10) {
  257. dev->mode_config.max_width = 2048;
  258. dev->mode_config.max_height = 2048;
  259. } else
  260. if (dev_priv->card_type < NV_50) {
  261. dev->mode_config.max_width = 4096;
  262. dev->mode_config.max_height = 4096;
  263. } else {
  264. dev->mode_config.max_width = 8192;
  265. dev->mode_config.max_height = 8192;
  266. }
  267. drm_kms_helper_poll_init(dev);
  268. drm_kms_helper_poll_disable(dev);
  269. ret = disp->create(dev);
  270. if (ret)
  271. return ret;
  272. if (dev->mode_config.num_crtc) {
  273. ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
  274. if (ret)
  275. return ret;
  276. }
  277. return ret;
  278. }
  279. void
  280. nouveau_display_destroy(struct drm_device *dev)
  281. {
  282. struct drm_nouveau_private *dev_priv = dev->dev_private;
  283. struct nouveau_display_engine *disp = &dev_priv->engine.display;
  284. drm_vblank_cleanup(dev);
  285. disp->destroy(dev);
  286. drm_kms_helper_poll_fini(dev);
  287. drm_mode_config_cleanup(dev);
  288. }
  289. int
  290. nouveau_vblank_enable(struct drm_device *dev, int crtc)
  291. {
  292. struct drm_nouveau_private *dev_priv = dev->dev_private;
  293. if (dev_priv->card_type >= NV_50)
  294. nv_mask(dev, NV50_PDISPLAY_INTR_EN_1, 0,
  295. NV50_PDISPLAY_INTR_EN_1_VBLANK_CRTC_(crtc));
  296. else
  297. NVWriteCRTC(dev, crtc, NV_PCRTC_INTR_EN_0,
  298. NV_PCRTC_INTR_0_VBLANK);
  299. return 0;
  300. }
  301. void
  302. nouveau_vblank_disable(struct drm_device *dev, int crtc)
  303. {
  304. struct drm_nouveau_private *dev_priv = dev->dev_private;
  305. if (dev_priv->card_type >= NV_50)
  306. nv_mask(dev, NV50_PDISPLAY_INTR_EN_1,
  307. NV50_PDISPLAY_INTR_EN_1_VBLANK_CRTC_(crtc), 0);
  308. else
  309. NVWriteCRTC(dev, crtc, NV_PCRTC_INTR_EN_0, 0);
  310. }
  311. static int
  312. nouveau_page_flip_reserve(struct nouveau_bo *old_bo,
  313. struct nouveau_bo *new_bo)
  314. {
  315. int ret;
  316. ret = nouveau_bo_pin(new_bo, TTM_PL_FLAG_VRAM);
  317. if (ret)
  318. return ret;
  319. ret = ttm_bo_reserve(&new_bo->bo, false, false, false, 0);
  320. if (ret)
  321. goto fail;
  322. ret = ttm_bo_reserve(&old_bo->bo, false, false, false, 0);
  323. if (ret)
  324. goto fail_unreserve;
  325. return 0;
  326. fail_unreserve:
  327. ttm_bo_unreserve(&new_bo->bo);
  328. fail:
  329. nouveau_bo_unpin(new_bo);
  330. return ret;
  331. }
  332. static void
  333. nouveau_page_flip_unreserve(struct nouveau_bo *old_bo,
  334. struct nouveau_bo *new_bo,
  335. struct nouveau_fence *fence)
  336. {
  337. nouveau_bo_fence(new_bo, fence);
  338. ttm_bo_unreserve(&new_bo->bo);
  339. nouveau_bo_fence(old_bo, fence);
  340. ttm_bo_unreserve(&old_bo->bo);
  341. nouveau_bo_unpin(old_bo);
  342. }
  343. static int
  344. nouveau_page_flip_emit(struct nouveau_channel *chan,
  345. struct nouveau_bo *old_bo,
  346. struct nouveau_bo *new_bo,
  347. struct nouveau_page_flip_state *s,
  348. struct nouveau_fence **pfence)
  349. {
  350. struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
  351. struct drm_device *dev = chan->dev;
  352. unsigned long flags;
  353. int ret;
  354. /* Queue it to the pending list */
  355. spin_lock_irqsave(&dev->event_lock, flags);
  356. list_add_tail(&s->head, &chan->nvsw.flip);
  357. spin_unlock_irqrestore(&dev->event_lock, flags);
  358. /* Synchronize with the old framebuffer */
  359. ret = nouveau_fence_sync(old_bo->bo.sync_obj, chan);
  360. if (ret)
  361. goto fail;
  362. /* Emit the pageflip */
  363. ret = RING_SPACE(chan, 2);
  364. if (ret)
  365. goto fail;
  366. if (dev_priv->card_type < NV_C0)
  367. BEGIN_RING(chan, NvSubSw, NV_SW_PAGE_FLIP, 1);
  368. else
  369. BEGIN_NVC0(chan, 2, NvSubM2MF, 0x0500, 1);
  370. OUT_RING (chan, 0);
  371. FIRE_RING (chan);
  372. ret = nouveau_fence_new(chan, pfence, true);
  373. if (ret)
  374. goto fail;
  375. return 0;
  376. fail:
  377. spin_lock_irqsave(&dev->event_lock, flags);
  378. list_del(&s->head);
  379. spin_unlock_irqrestore(&dev->event_lock, flags);
  380. return ret;
  381. }
  382. int
  383. nouveau_crtc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  384. struct drm_pending_vblank_event *event)
  385. {
  386. struct drm_device *dev = crtc->dev;
  387. struct drm_nouveau_private *dev_priv = dev->dev_private;
  388. struct nouveau_bo *old_bo = nouveau_framebuffer(crtc->fb)->nvbo;
  389. struct nouveau_bo *new_bo = nouveau_framebuffer(fb)->nvbo;
  390. struct nouveau_page_flip_state *s;
  391. struct nouveau_channel *chan;
  392. struct nouveau_fence *fence;
  393. int ret;
  394. if (!dev_priv->channel)
  395. return -ENODEV;
  396. s = kzalloc(sizeof(*s), GFP_KERNEL);
  397. if (!s)
  398. return -ENOMEM;
  399. /* Don't let the buffers go away while we flip */
  400. ret = nouveau_page_flip_reserve(old_bo, new_bo);
  401. if (ret)
  402. goto fail_free;
  403. /* Initialize a page flip struct */
  404. *s = (struct nouveau_page_flip_state)
  405. { { }, event, nouveau_crtc(crtc)->index,
  406. fb->bits_per_pixel, fb->pitches[0], crtc->x, crtc->y,
  407. new_bo->bo.offset };
  408. /* Choose the channel the flip will be handled in */
  409. chan = nouveau_fence_channel(new_bo->bo.sync_obj);
  410. if (!chan)
  411. chan = nouveau_channel_get_unlocked(dev_priv->channel);
  412. mutex_lock(&chan->mutex);
  413. /* Emit a page flip */
  414. if (dev_priv->card_type >= NV_50) {
  415. if (dev_priv->card_type >= NV_D0)
  416. ret = nvd0_display_flip_next(crtc, fb, chan, 0);
  417. else
  418. ret = nv50_display_flip_next(crtc, fb, chan);
  419. if (ret) {
  420. nouveau_channel_put(&chan);
  421. goto fail_unreserve;
  422. }
  423. }
  424. ret = nouveau_page_flip_emit(chan, old_bo, new_bo, s, &fence);
  425. nouveau_channel_put(&chan);
  426. if (ret)
  427. goto fail_unreserve;
  428. /* Update the crtc struct and cleanup */
  429. crtc->fb = fb;
  430. nouveau_page_flip_unreserve(old_bo, new_bo, fence);
  431. nouveau_fence_unref(&fence);
  432. return 0;
  433. fail_unreserve:
  434. nouveau_page_flip_unreserve(old_bo, new_bo, NULL);
  435. fail_free:
  436. kfree(s);
  437. return ret;
  438. }
  439. int
  440. nouveau_finish_page_flip(struct nouveau_channel *chan,
  441. struct nouveau_page_flip_state *ps)
  442. {
  443. struct drm_device *dev = chan->dev;
  444. struct nouveau_page_flip_state *s;
  445. unsigned long flags;
  446. spin_lock_irqsave(&dev->event_lock, flags);
  447. if (list_empty(&chan->nvsw.flip)) {
  448. NV_ERROR(dev, "Unexpected pageflip in channel %d.\n", chan->id);
  449. spin_unlock_irqrestore(&dev->event_lock, flags);
  450. return -EINVAL;
  451. }
  452. s = list_first_entry(&chan->nvsw.flip,
  453. struct nouveau_page_flip_state, head);
  454. if (s->event) {
  455. struct drm_pending_vblank_event *e = s->event;
  456. struct timeval now;
  457. do_gettimeofday(&now);
  458. e->event.sequence = 0;
  459. e->event.tv_sec = now.tv_sec;
  460. e->event.tv_usec = now.tv_usec;
  461. list_add_tail(&e->base.link, &e->base.file_priv->event_list);
  462. wake_up_interruptible(&e->base.file_priv->event_wait);
  463. }
  464. list_del(&s->head);
  465. if (ps)
  466. *ps = *s;
  467. kfree(s);
  468. spin_unlock_irqrestore(&dev->event_lock, flags);
  469. return 0;
  470. }
  471. int
  472. nouveau_display_dumb_create(struct drm_file *file_priv, struct drm_device *dev,
  473. struct drm_mode_create_dumb *args)
  474. {
  475. struct nouveau_bo *bo;
  476. int ret;
  477. args->pitch = roundup(args->width * (args->bpp / 8), 256);
  478. args->size = args->pitch * args->height;
  479. args->size = roundup(args->size, PAGE_SIZE);
  480. ret = nouveau_gem_new(dev, args->size, 0, TTM_PL_FLAG_VRAM, 0, 0, &bo);
  481. if (ret)
  482. return ret;
  483. ret = drm_gem_handle_create(file_priv, bo->gem, &args->handle);
  484. drm_gem_object_unreference_unlocked(bo->gem);
  485. return ret;
  486. }
  487. int
  488. nouveau_display_dumb_destroy(struct drm_file *file_priv, struct drm_device *dev,
  489. uint32_t handle)
  490. {
  491. return drm_gem_handle_delete(file_priv, handle);
  492. }
  493. int
  494. nouveau_display_dumb_map_offset(struct drm_file *file_priv,
  495. struct drm_device *dev,
  496. uint32_t handle, uint64_t *poffset)
  497. {
  498. struct drm_gem_object *gem;
  499. gem = drm_gem_object_lookup(dev, file_priv, handle);
  500. if (gem) {
  501. struct nouveau_bo *bo = gem->driver_private;
  502. *poffset = bo->bo.addr_space_offset;
  503. drm_gem_object_unreference_unlocked(gem);
  504. return 0;
  505. }
  506. return -ENOENT;
  507. }