nouveau_display.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. if (dev_priv->card_type < NV_D0)
  96. nv_fb->r_pitch = 0x00100000 | fb->pitch;
  97. else
  98. nv_fb->r_pitch = 0x01000000 | fb->pitch;
  99. } else {
  100. u32 mode = nvbo->tile_mode;
  101. if (dev_priv->card_type >= NV_C0)
  102. mode >>= 4;
  103. nv_fb->r_pitch = ((fb->pitch / 4) << 4) | mode;
  104. }
  105. }
  106. return 0;
  107. }
  108. static struct drm_framebuffer *
  109. nouveau_user_framebuffer_create(struct drm_device *dev,
  110. struct drm_file *file_priv,
  111. struct drm_mode_fb_cmd *mode_cmd)
  112. {
  113. struct nouveau_framebuffer *nouveau_fb;
  114. struct drm_gem_object *gem;
  115. int ret;
  116. gem = drm_gem_object_lookup(dev, file_priv, mode_cmd->handle);
  117. if (!gem)
  118. return ERR_PTR(-ENOENT);
  119. nouveau_fb = kzalloc(sizeof(struct nouveau_framebuffer), GFP_KERNEL);
  120. if (!nouveau_fb)
  121. return ERR_PTR(-ENOMEM);
  122. ret = nouveau_framebuffer_init(dev, nouveau_fb, mode_cmd, nouveau_gem_object(gem));
  123. if (ret) {
  124. drm_gem_object_unreference(gem);
  125. return ERR_PTR(ret);
  126. }
  127. return &nouveau_fb->base;
  128. }
  129. const struct drm_mode_config_funcs nouveau_mode_config_funcs = {
  130. .fb_create = nouveau_user_framebuffer_create,
  131. .output_poll_changed = nouveau_fbcon_output_poll_changed,
  132. };
  133. int
  134. nouveau_vblank_enable(struct drm_device *dev, int crtc)
  135. {
  136. struct drm_nouveau_private *dev_priv = dev->dev_private;
  137. if (dev_priv->card_type >= NV_50)
  138. nv_mask(dev, NV50_PDISPLAY_INTR_EN_1, 0,
  139. NV50_PDISPLAY_INTR_EN_1_VBLANK_CRTC_(crtc));
  140. else
  141. NVWriteCRTC(dev, crtc, NV_PCRTC_INTR_EN_0,
  142. NV_PCRTC_INTR_0_VBLANK);
  143. return 0;
  144. }
  145. void
  146. nouveau_vblank_disable(struct drm_device *dev, int crtc)
  147. {
  148. struct drm_nouveau_private *dev_priv = dev->dev_private;
  149. if (dev_priv->card_type >= NV_50)
  150. nv_mask(dev, NV50_PDISPLAY_INTR_EN_1,
  151. NV50_PDISPLAY_INTR_EN_1_VBLANK_CRTC_(crtc), 0);
  152. else
  153. NVWriteCRTC(dev, crtc, NV_PCRTC_INTR_EN_0, 0);
  154. }
  155. static int
  156. nouveau_page_flip_reserve(struct nouveau_bo *old_bo,
  157. struct nouveau_bo *new_bo)
  158. {
  159. int ret;
  160. ret = nouveau_bo_pin(new_bo, TTM_PL_FLAG_VRAM);
  161. if (ret)
  162. return ret;
  163. ret = ttm_bo_reserve(&new_bo->bo, false, false, false, 0);
  164. if (ret)
  165. goto fail;
  166. ret = ttm_bo_reserve(&old_bo->bo, false, false, false, 0);
  167. if (ret)
  168. goto fail_unreserve;
  169. return 0;
  170. fail_unreserve:
  171. ttm_bo_unreserve(&new_bo->bo);
  172. fail:
  173. nouveau_bo_unpin(new_bo);
  174. return ret;
  175. }
  176. static void
  177. nouveau_page_flip_unreserve(struct nouveau_bo *old_bo,
  178. struct nouveau_bo *new_bo,
  179. struct nouveau_fence *fence)
  180. {
  181. nouveau_bo_fence(new_bo, fence);
  182. ttm_bo_unreserve(&new_bo->bo);
  183. nouveau_bo_fence(old_bo, fence);
  184. ttm_bo_unreserve(&old_bo->bo);
  185. nouveau_bo_unpin(old_bo);
  186. }
  187. static int
  188. nouveau_page_flip_emit(struct nouveau_channel *chan,
  189. struct nouveau_bo *old_bo,
  190. struct nouveau_bo *new_bo,
  191. struct nouveau_page_flip_state *s,
  192. struct nouveau_fence **pfence)
  193. {
  194. struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
  195. struct drm_device *dev = chan->dev;
  196. unsigned long flags;
  197. int ret;
  198. /* Queue it to the pending list */
  199. spin_lock_irqsave(&dev->event_lock, flags);
  200. list_add_tail(&s->head, &chan->nvsw.flip);
  201. spin_unlock_irqrestore(&dev->event_lock, flags);
  202. /* Synchronize with the old framebuffer */
  203. ret = nouveau_fence_sync(old_bo->bo.sync_obj, chan);
  204. if (ret)
  205. goto fail;
  206. /* Emit the pageflip */
  207. ret = RING_SPACE(chan, 2);
  208. if (ret)
  209. goto fail;
  210. if (dev_priv->card_type < NV_C0)
  211. BEGIN_RING(chan, NvSubSw, NV_SW_PAGE_FLIP, 1);
  212. else
  213. BEGIN_NVC0(chan, 2, NvSubM2MF, 0x0500, 1);
  214. OUT_RING (chan, 0);
  215. FIRE_RING (chan);
  216. ret = nouveau_fence_new(chan, pfence, true);
  217. if (ret)
  218. goto fail;
  219. return 0;
  220. fail:
  221. spin_lock_irqsave(&dev->event_lock, flags);
  222. list_del(&s->head);
  223. spin_unlock_irqrestore(&dev->event_lock, flags);
  224. return ret;
  225. }
  226. int
  227. nouveau_crtc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  228. struct drm_pending_vblank_event *event)
  229. {
  230. struct drm_device *dev = crtc->dev;
  231. struct drm_nouveau_private *dev_priv = dev->dev_private;
  232. struct nouveau_bo *old_bo = nouveau_framebuffer(crtc->fb)->nvbo;
  233. struct nouveau_bo *new_bo = nouveau_framebuffer(fb)->nvbo;
  234. struct nouveau_page_flip_state *s;
  235. struct nouveau_channel *chan;
  236. struct nouveau_fence *fence;
  237. int ret;
  238. if (!dev_priv->channel)
  239. return -ENODEV;
  240. s = kzalloc(sizeof(*s), GFP_KERNEL);
  241. if (!s)
  242. return -ENOMEM;
  243. /* Don't let the buffers go away while we flip */
  244. ret = nouveau_page_flip_reserve(old_bo, new_bo);
  245. if (ret)
  246. goto fail_free;
  247. /* Initialize a page flip struct */
  248. *s = (struct nouveau_page_flip_state)
  249. { { }, event, nouveau_crtc(crtc)->index,
  250. fb->bits_per_pixel, fb->pitch, crtc->x, crtc->y,
  251. new_bo->bo.offset };
  252. /* Choose the channel the flip will be handled in */
  253. chan = nouveau_fence_channel(new_bo->bo.sync_obj);
  254. if (!chan)
  255. chan = nouveau_channel_get_unlocked(dev_priv->channel);
  256. mutex_lock(&chan->mutex);
  257. /* Emit a page flip */
  258. if (dev_priv->card_type >= NV_50) {
  259. ret = nv50_display_flip_next(crtc, fb, chan);
  260. if (ret) {
  261. nouveau_channel_put(&chan);
  262. goto fail_unreserve;
  263. }
  264. }
  265. ret = nouveau_page_flip_emit(chan, old_bo, new_bo, s, &fence);
  266. nouveau_channel_put(&chan);
  267. if (ret)
  268. goto fail_unreserve;
  269. /* Update the crtc struct and cleanup */
  270. crtc->fb = fb;
  271. nouveau_page_flip_unreserve(old_bo, new_bo, fence);
  272. nouveau_fence_unref(&fence);
  273. return 0;
  274. fail_unreserve:
  275. nouveau_page_flip_unreserve(old_bo, new_bo, NULL);
  276. fail_free:
  277. kfree(s);
  278. return ret;
  279. }
  280. int
  281. nouveau_finish_page_flip(struct nouveau_channel *chan,
  282. struct nouveau_page_flip_state *ps)
  283. {
  284. struct drm_device *dev = chan->dev;
  285. struct nouveau_page_flip_state *s;
  286. unsigned long flags;
  287. spin_lock_irqsave(&dev->event_lock, flags);
  288. if (list_empty(&chan->nvsw.flip)) {
  289. NV_ERROR(dev, "Unexpected pageflip in channel %d.\n", chan->id);
  290. spin_unlock_irqrestore(&dev->event_lock, flags);
  291. return -EINVAL;
  292. }
  293. s = list_first_entry(&chan->nvsw.flip,
  294. struct nouveau_page_flip_state, head);
  295. if (s->event) {
  296. struct drm_pending_vblank_event *e = s->event;
  297. struct timeval now;
  298. do_gettimeofday(&now);
  299. e->event.sequence = 0;
  300. e->event.tv_sec = now.tv_sec;
  301. e->event.tv_usec = now.tv_usec;
  302. list_add_tail(&e->base.link, &e->base.file_priv->event_list);
  303. wake_up_interruptible(&e->base.file_priv->event_wait);
  304. }
  305. list_del(&s->head);
  306. if (ps)
  307. *ps = *s;
  308. kfree(s);
  309. spin_unlock_irqrestore(&dev->event_lock, flags);
  310. return 0;
  311. }