nouveau_display.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 "nv50_display.h"
  35. static void
  36. nouveau_user_framebuffer_destroy(struct drm_framebuffer *drm_fb)
  37. {
  38. struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
  39. if (fb->nvbo)
  40. drm_gem_object_unreference_unlocked(fb->nvbo->gem);
  41. drm_framebuffer_cleanup(drm_fb);
  42. kfree(fb);
  43. }
  44. static int
  45. nouveau_user_framebuffer_create_handle(struct drm_framebuffer *drm_fb,
  46. struct drm_file *file_priv,
  47. unsigned int *handle)
  48. {
  49. struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
  50. return drm_gem_handle_create(file_priv, fb->nvbo->gem, handle);
  51. }
  52. static const struct drm_framebuffer_funcs nouveau_framebuffer_funcs = {
  53. .destroy = nouveau_user_framebuffer_destroy,
  54. .create_handle = nouveau_user_framebuffer_create_handle,
  55. };
  56. int
  57. nouveau_framebuffer_init(struct drm_device *dev,
  58. struct nouveau_framebuffer *nv_fb,
  59. struct drm_mode_fb_cmd *mode_cmd,
  60. struct nouveau_bo *nvbo)
  61. {
  62. struct drm_nouveau_private *dev_priv = dev->dev_private;
  63. struct drm_framebuffer *fb = &nv_fb->base;
  64. int ret;
  65. ret = drm_framebuffer_init(dev, fb, &nouveau_framebuffer_funcs);
  66. if (ret) {
  67. return ret;
  68. }
  69. drm_helper_mode_fill_fb_struct(fb, mode_cmd);
  70. nv_fb->nvbo = nvbo;
  71. if (dev_priv->card_type >= NV_50) {
  72. u32 tile_flags = nouveau_bo_tile_layout(nvbo);
  73. if (tile_flags == 0x7a00 ||
  74. tile_flags == 0xfe00)
  75. nv_fb->r_dma = NvEvoFB32;
  76. else
  77. if (tile_flags == 0x7000)
  78. nv_fb->r_dma = NvEvoFB16;
  79. else
  80. nv_fb->r_dma = NvEvoVRAM_LP;
  81. switch (fb->depth) {
  82. case 8: nv_fb->r_format = NV50_EVO_CRTC_FB_DEPTH_8; break;
  83. case 15: nv_fb->r_format = NV50_EVO_CRTC_FB_DEPTH_15; break;
  84. case 16: nv_fb->r_format = NV50_EVO_CRTC_FB_DEPTH_16; break;
  85. case 24:
  86. case 32: nv_fb->r_format = NV50_EVO_CRTC_FB_DEPTH_24; break;
  87. case 30: nv_fb->r_format = NV50_EVO_CRTC_FB_DEPTH_30; break;
  88. default:
  89. NV_ERROR(dev, "unknown depth %d\n", fb->depth);
  90. return -EINVAL;
  91. }
  92. if (dev_priv->chipset == 0x50)
  93. nv_fb->r_format |= (tile_flags << 8);
  94. if (!tile_flags)
  95. nv_fb->r_pitch = 0x00100000 | fb->pitch;
  96. else {
  97. u32 mode = nvbo->tile_mode;
  98. if (dev_priv->card_type >= NV_C0)
  99. mode >>= 4;
  100. nv_fb->r_pitch = ((fb->pitch / 4) << 4) | mode;
  101. }
  102. }
  103. return 0;
  104. }
  105. static struct drm_framebuffer *
  106. nouveau_user_framebuffer_create(struct drm_device *dev,
  107. struct drm_file *file_priv,
  108. struct drm_mode_fb_cmd *mode_cmd)
  109. {
  110. struct nouveau_framebuffer *nouveau_fb;
  111. struct drm_gem_object *gem;
  112. int ret;
  113. gem = drm_gem_object_lookup(dev, file_priv, mode_cmd->handle);
  114. if (!gem)
  115. return ERR_PTR(-ENOENT);
  116. nouveau_fb = kzalloc(sizeof(struct nouveau_framebuffer), GFP_KERNEL);
  117. if (!nouveau_fb)
  118. return ERR_PTR(-ENOMEM);
  119. ret = nouveau_framebuffer_init(dev, nouveau_fb, mode_cmd, nouveau_gem_object(gem));
  120. if (ret) {
  121. drm_gem_object_unreference(gem);
  122. return ERR_PTR(ret);
  123. }
  124. return &nouveau_fb->base;
  125. }
  126. const struct drm_mode_config_funcs nouveau_mode_config_funcs = {
  127. .fb_create = nouveau_user_framebuffer_create,
  128. .output_poll_changed = nouveau_fbcon_output_poll_changed,
  129. };
  130. int
  131. nouveau_vblank_enable(struct drm_device *dev, int crtc)
  132. {
  133. struct drm_nouveau_private *dev_priv = dev->dev_private;
  134. if (dev_priv->card_type >= NV_50)
  135. nv_mask(dev, NV50_PDISPLAY_INTR_EN_1, 0,
  136. NV50_PDISPLAY_INTR_EN_1_VBLANK_CRTC_(crtc));
  137. else
  138. NVWriteCRTC(dev, crtc, NV_PCRTC_INTR_EN_0,
  139. NV_PCRTC_INTR_0_VBLANK);
  140. return 0;
  141. }
  142. void
  143. nouveau_vblank_disable(struct drm_device *dev, int crtc)
  144. {
  145. struct drm_nouveau_private *dev_priv = dev->dev_private;
  146. if (dev_priv->card_type >= NV_50)
  147. nv_mask(dev, NV50_PDISPLAY_INTR_EN_1,
  148. NV50_PDISPLAY_INTR_EN_1_VBLANK_CRTC_(crtc), 0);
  149. else
  150. NVWriteCRTC(dev, crtc, NV_PCRTC_INTR_EN_0, 0);
  151. }
  152. static int
  153. nouveau_page_flip_reserve(struct nouveau_bo *old_bo,
  154. struct nouveau_bo *new_bo)
  155. {
  156. int ret;
  157. ret = nouveau_bo_pin(new_bo, TTM_PL_FLAG_VRAM);
  158. if (ret)
  159. return ret;
  160. ret = ttm_bo_reserve(&new_bo->bo, false, false, false, 0);
  161. if (ret)
  162. goto fail;
  163. ret = ttm_bo_reserve(&old_bo->bo, false, false, false, 0);
  164. if (ret)
  165. goto fail_unreserve;
  166. return 0;
  167. fail_unreserve:
  168. ttm_bo_unreserve(&new_bo->bo);
  169. fail:
  170. nouveau_bo_unpin(new_bo);
  171. return ret;
  172. }
  173. static void
  174. nouveau_page_flip_unreserve(struct nouveau_bo *old_bo,
  175. struct nouveau_bo *new_bo,
  176. struct nouveau_fence *fence)
  177. {
  178. nouveau_bo_fence(new_bo, fence);
  179. ttm_bo_unreserve(&new_bo->bo);
  180. nouveau_bo_fence(old_bo, fence);
  181. ttm_bo_unreserve(&old_bo->bo);
  182. nouveau_bo_unpin(old_bo);
  183. }
  184. static int
  185. nouveau_page_flip_emit(struct nouveau_channel *chan,
  186. struct nouveau_bo *old_bo,
  187. struct nouveau_bo *new_bo,
  188. struct nouveau_page_flip_state *s,
  189. struct nouveau_fence **pfence)
  190. {
  191. struct drm_device *dev = chan->dev;
  192. unsigned long flags;
  193. int ret;
  194. /* Queue it to the pending list */
  195. spin_lock_irqsave(&dev->event_lock, flags);
  196. list_add_tail(&s->head, &chan->nvsw.flip);
  197. spin_unlock_irqrestore(&dev->event_lock, flags);
  198. /* Synchronize with the old framebuffer */
  199. ret = nouveau_fence_sync(old_bo->bo.sync_obj, chan);
  200. if (ret)
  201. goto fail;
  202. /* Emit the pageflip */
  203. ret = RING_SPACE(chan, 2);
  204. if (ret)
  205. goto fail;
  206. BEGIN_RING(chan, NvSubSw, NV_SW_PAGE_FLIP, 1);
  207. OUT_RING(chan, 0);
  208. FIRE_RING(chan);
  209. ret = nouveau_fence_new(chan, pfence, true);
  210. if (ret)
  211. goto fail;
  212. return 0;
  213. fail:
  214. spin_lock_irqsave(&dev->event_lock, flags);
  215. list_del(&s->head);
  216. spin_unlock_irqrestore(&dev->event_lock, flags);
  217. return ret;
  218. }
  219. int
  220. nouveau_crtc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  221. struct drm_pending_vblank_event *event)
  222. {
  223. struct drm_device *dev = crtc->dev;
  224. struct drm_nouveau_private *dev_priv = dev->dev_private;
  225. struct nouveau_bo *old_bo = nouveau_framebuffer(crtc->fb)->nvbo;
  226. struct nouveau_bo *new_bo = nouveau_framebuffer(fb)->nvbo;
  227. struct nouveau_page_flip_state *s;
  228. struct nouveau_channel *chan;
  229. struct nouveau_fence *fence;
  230. int ret;
  231. if (dev_priv->engine.graph.accel_blocked)
  232. return -ENODEV;
  233. s = kzalloc(sizeof(*s), GFP_KERNEL);
  234. if (!s)
  235. return -ENOMEM;
  236. /* Don't let the buffers go away while we flip */
  237. ret = nouveau_page_flip_reserve(old_bo, new_bo);
  238. if (ret)
  239. goto fail_free;
  240. /* Initialize a page flip struct */
  241. *s = (struct nouveau_page_flip_state)
  242. { { }, s->event, nouveau_crtc(crtc)->index,
  243. fb->bits_per_pixel, fb->pitch, crtc->x, crtc->y,
  244. new_bo->bo.offset };
  245. /* Choose the channel the flip will be handled in */
  246. chan = nouveau_fence_channel(new_bo->bo.sync_obj);
  247. if (!chan)
  248. chan = nouveau_channel_get_unlocked(dev_priv->channel);
  249. mutex_lock(&chan->mutex);
  250. /* Emit a page flip */
  251. if (dev_priv->card_type >= NV_50) {
  252. ret = nv50_display_flip_next(crtc, fb, chan);
  253. if (ret) {
  254. nouveau_channel_put(&chan);
  255. goto fail_unreserve;
  256. }
  257. }
  258. ret = nouveau_page_flip_emit(chan, old_bo, new_bo, s, &fence);
  259. nouveau_channel_put(&chan);
  260. if (ret)
  261. goto fail_unreserve;
  262. /* Update the crtc struct and cleanup */
  263. crtc->fb = fb;
  264. nouveau_page_flip_unreserve(old_bo, new_bo, fence);
  265. nouveau_fence_unref(&fence);
  266. return 0;
  267. fail_unreserve:
  268. nouveau_page_flip_unreserve(old_bo, new_bo, NULL);
  269. fail_free:
  270. kfree(s);
  271. return ret;
  272. }
  273. int
  274. nouveau_finish_page_flip(struct nouveau_channel *chan,
  275. struct nouveau_page_flip_state *ps)
  276. {
  277. struct drm_device *dev = chan->dev;
  278. struct nouveau_page_flip_state *s;
  279. unsigned long flags;
  280. spin_lock_irqsave(&dev->event_lock, flags);
  281. if (list_empty(&chan->nvsw.flip)) {
  282. NV_ERROR(dev, "Unexpected pageflip in channel %d.\n", chan->id);
  283. spin_unlock_irqrestore(&dev->event_lock, flags);
  284. return -EINVAL;
  285. }
  286. s = list_first_entry(&chan->nvsw.flip,
  287. struct nouveau_page_flip_state, head);
  288. if (s->event) {
  289. struct drm_pending_vblank_event *e = s->event;
  290. struct timeval now;
  291. do_gettimeofday(&now);
  292. e->event.sequence = 0;
  293. e->event.tv_sec = now.tv_sec;
  294. e->event.tv_usec = now.tv_usec;
  295. list_add_tail(&e->base.link, &e->base.file_priv->event_list);
  296. wake_up_interruptible(&e->base.file_priv->event_wait);
  297. }
  298. list_del(&s->head);
  299. if (ps)
  300. *ps = *s;
  301. kfree(s);
  302. spin_unlock_irqrestore(&dev->event_lock, flags);
  303. return 0;
  304. }