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