nouveau_display.c 14 KB

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