Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2009 Red Hat Inc. |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 | * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 18 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 | * OTHER DEALINGS IN THE SOFTWARE. |
| 21 | * |
| 22 | * Author: Ben Skeggs |
| 23 | */ |
| 24 | |
| 25 | #include "drmP.h" |
| 26 | #include "drm.h" |
| 27 | #include "drm_crtc_helper.h" |
| 28 | |
| 29 | #include "nouveau_drv.h" |
| 30 | #include "nouveau_fb.h" |
| 31 | #include "nouveau_hw.h" |
| 32 | #include "nouveau_encoder.h" |
| 33 | #include "nouveau_connector.h" |
| 34 | |
| 35 | #define MULTIPLE_ENCODERS(e) (e & (e - 1)) |
| 36 | |
| 37 | static void |
| 38 | nv04_display_store_initial_head_owner(struct drm_device *dev) |
| 39 | { |
| 40 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
| 41 | |
| 42 | if (dev_priv->chipset != 0x11) { |
| 43 | dev_priv->crtc_owner = NVReadVgaCrtc(dev, 0, NV_CIO_CRE_44); |
| 44 | goto ownerknown; |
| 45 | } |
| 46 | |
| 47 | /* reading CR44 is broken on nv11, so we attempt to infer it */ |
| 48 | if (nvReadMC(dev, NV_PBUS_DEBUG_1) & (1 << 28)) /* heads tied, restore both */ |
| 49 | dev_priv->crtc_owner = 0x4; |
| 50 | else { |
| 51 | uint8_t slaved_on_A, slaved_on_B; |
| 52 | bool tvA = false; |
| 53 | bool tvB = false; |
| 54 | |
| 55 | NVLockVgaCrtcs(dev, false); |
| 56 | |
| 57 | slaved_on_B = NVReadVgaCrtc(dev, 1, NV_CIO_CRE_PIXEL_INDEX) & |
| 58 | 0x80; |
| 59 | if (slaved_on_B) |
| 60 | tvB = !(NVReadVgaCrtc(dev, 1, NV_CIO_CRE_LCD__INDEX) & |
| 61 | MASK(NV_CIO_CRE_LCD_LCD_SELECT)); |
| 62 | |
| 63 | slaved_on_A = NVReadVgaCrtc(dev, 0, NV_CIO_CRE_PIXEL_INDEX) & |
| 64 | 0x80; |
| 65 | if (slaved_on_A) |
| 66 | tvA = !(NVReadVgaCrtc(dev, 0, NV_CIO_CRE_LCD__INDEX) & |
| 67 | MASK(NV_CIO_CRE_LCD_LCD_SELECT)); |
| 68 | |
| 69 | NVLockVgaCrtcs(dev, true); |
| 70 | |
| 71 | if (slaved_on_A && !tvA) |
| 72 | dev_priv->crtc_owner = 0x0; |
| 73 | else if (slaved_on_B && !tvB) |
| 74 | dev_priv->crtc_owner = 0x3; |
| 75 | else if (slaved_on_A) |
| 76 | dev_priv->crtc_owner = 0x0; |
| 77 | else if (slaved_on_B) |
| 78 | dev_priv->crtc_owner = 0x3; |
| 79 | else |
| 80 | dev_priv->crtc_owner = 0x0; |
| 81 | } |
| 82 | |
| 83 | ownerknown: |
| 84 | NV_INFO(dev, "Initial CRTC_OWNER is %d\n", dev_priv->crtc_owner); |
| 85 | |
| 86 | /* we need to ensure the heads are not tied henceforth, or reading any |
| 87 | * 8 bit reg on head B will fail |
| 88 | * setting a single arbitrary head solves that */ |
| 89 | NVSetOwner(dev, 0); |
| 90 | } |
| 91 | |
| 92 | int |
| 93 | nv04_display_create(struct drm_device *dev) |
| 94 | { |
| 95 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
| 96 | struct parsed_dcb *dcb = dev_priv->vbios->dcb; |
| 97 | struct drm_encoder *encoder; |
| 98 | struct drm_crtc *crtc; |
| 99 | uint16_t connector[16] = { 0 }; |
| 100 | int i, ret; |
| 101 | |
| 102 | NV_DEBUG(dev, "\n"); |
| 103 | |
| 104 | if (nv_two_heads(dev)) |
| 105 | nv04_display_store_initial_head_owner(dev); |
Francisco Jerez | 95f158e | 2009-12-11 23:44:49 +0100 | [diff] [blame^] | 106 | nouveau_hw_save_vga_fonts(dev, 1); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 107 | |
| 108 | drm_mode_config_init(dev); |
| 109 | drm_mode_create_scaling_mode_property(dev); |
| 110 | drm_mode_create_dithering_property(dev); |
| 111 | |
| 112 | dev->mode_config.funcs = (void *)&nouveau_mode_config_funcs; |
| 113 | |
| 114 | dev->mode_config.min_width = 0; |
| 115 | dev->mode_config.min_height = 0; |
| 116 | switch (dev_priv->card_type) { |
| 117 | case NV_04: |
| 118 | dev->mode_config.max_width = 2048; |
| 119 | dev->mode_config.max_height = 2048; |
| 120 | break; |
| 121 | default: |
| 122 | dev->mode_config.max_width = 4096; |
| 123 | dev->mode_config.max_height = 4096; |
| 124 | break; |
| 125 | } |
| 126 | |
| 127 | dev->mode_config.fb_base = dev_priv->fb_phys; |
| 128 | |
| 129 | nv04_crtc_create(dev, 0); |
| 130 | if (nv_two_heads(dev)) |
| 131 | nv04_crtc_create(dev, 1); |
| 132 | |
| 133 | for (i = 0; i < dcb->entries; i++) { |
| 134 | struct dcb_entry *dcbent = &dcb->entry[i]; |
| 135 | |
| 136 | switch (dcbent->type) { |
| 137 | case OUTPUT_ANALOG: |
| 138 | ret = nv04_dac_create(dev, dcbent); |
| 139 | break; |
| 140 | case OUTPUT_LVDS: |
| 141 | case OUTPUT_TMDS: |
| 142 | ret = nv04_dfp_create(dev, dcbent); |
| 143 | break; |
| 144 | case OUTPUT_TV: |
| 145 | if (dcbent->location == DCB_LOC_ON_CHIP) |
| 146 | ret = nv17_tv_create(dev, dcbent); |
| 147 | else |
| 148 | ret = nv04_tv_create(dev, dcbent); |
| 149 | break; |
| 150 | default: |
| 151 | NV_WARN(dev, "DCB type %d not known\n", dcbent->type); |
| 152 | continue; |
| 153 | } |
| 154 | |
| 155 | if (ret) |
| 156 | continue; |
| 157 | |
| 158 | connector[dcbent->connector] |= (1 << dcbent->type); |
| 159 | } |
| 160 | |
| 161 | for (i = 0; i < dcb->entries; i++) { |
| 162 | struct dcb_entry *dcbent = &dcb->entry[i]; |
| 163 | uint16_t encoders; |
| 164 | int type; |
| 165 | |
| 166 | encoders = connector[dcbent->connector]; |
| 167 | if (!(encoders & (1 << dcbent->type))) |
| 168 | continue; |
| 169 | connector[dcbent->connector] = 0; |
| 170 | |
| 171 | switch (dcbent->type) { |
| 172 | case OUTPUT_ANALOG: |
| 173 | if (!MULTIPLE_ENCODERS(encoders)) |
| 174 | type = DRM_MODE_CONNECTOR_VGA; |
| 175 | else |
| 176 | type = DRM_MODE_CONNECTOR_DVII; |
| 177 | break; |
| 178 | case OUTPUT_TMDS: |
| 179 | if (!MULTIPLE_ENCODERS(encoders)) |
| 180 | type = DRM_MODE_CONNECTOR_DVID; |
| 181 | else |
| 182 | type = DRM_MODE_CONNECTOR_DVII; |
| 183 | break; |
| 184 | case OUTPUT_LVDS: |
| 185 | type = DRM_MODE_CONNECTOR_LVDS; |
| 186 | #if 0 |
| 187 | /* don't create i2c adapter when lvds ddc not allowed */ |
| 188 | if (dcbent->lvdsconf.use_straps_for_mode || |
| 189 | dev_priv->vbios->fp_no_ddc) |
| 190 | i2c_index = 0xf; |
| 191 | #endif |
| 192 | break; |
| 193 | case OUTPUT_TV: |
| 194 | type = DRM_MODE_CONNECTOR_TV; |
| 195 | break; |
| 196 | default: |
| 197 | type = DRM_MODE_CONNECTOR_Unknown; |
| 198 | continue; |
| 199 | } |
| 200 | |
| 201 | nouveau_connector_create(dev, dcbent->connector, type); |
| 202 | } |
| 203 | |
| 204 | /* Save previous state */ |
| 205 | NVLockVgaCrtcs(dev, false); |
| 206 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 207 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) |
| 208 | crtc->funcs->save(crtc); |
| 209 | |
| 210 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { |
| 211 | struct drm_encoder_helper_funcs *func = encoder->helper_private; |
| 212 | |
| 213 | func->save(encoder); |
| 214 | } |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | void |
| 220 | nv04_display_destroy(struct drm_device *dev) |
| 221 | { |
| 222 | struct drm_encoder *encoder; |
| 223 | struct drm_crtc *crtc; |
| 224 | |
| 225 | NV_DEBUG(dev, "\n"); |
| 226 | |
| 227 | /* Turn every CRTC off. */ |
| 228 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { |
| 229 | struct drm_mode_set modeset = { |
| 230 | .crtc = crtc, |
| 231 | }; |
| 232 | |
| 233 | crtc->funcs->set_config(&modeset); |
| 234 | } |
| 235 | |
| 236 | /* Restore state */ |
| 237 | NVLockVgaCrtcs(dev, false); |
| 238 | |
| 239 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { |
| 240 | struct drm_encoder_helper_funcs *func = encoder->helper_private; |
| 241 | |
| 242 | func->restore(encoder); |
| 243 | } |
| 244 | |
| 245 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) |
| 246 | crtc->funcs->restore(crtc); |
| 247 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 248 | drm_mode_config_cleanup(dev); |
Francisco Jerez | 95f158e | 2009-12-11 23:44:49 +0100 | [diff] [blame^] | 249 | |
| 250 | nouveau_hw_save_vga_fonts(dev, 0); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | void |
| 254 | nv04_display_restore(struct drm_device *dev) |
| 255 | { |
| 256 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
| 257 | struct drm_encoder *encoder; |
| 258 | struct drm_crtc *crtc; |
| 259 | |
| 260 | NVLockVgaCrtcs(dev, false); |
| 261 | |
| 262 | /* meh.. modeset apparently doesn't setup all the regs and depends |
| 263 | * on pre-existing state, for now load the state of the card *before* |
| 264 | * nouveau was loaded, and then do a modeset. |
| 265 | * |
| 266 | * best thing to do probably is to make save/restore routines not |
| 267 | * save/restore "pre-load" state, but more general so we can save |
| 268 | * on suspend too. |
| 269 | */ |
| 270 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { |
| 271 | struct drm_encoder_helper_funcs *func = encoder->helper_private; |
| 272 | |
| 273 | func->restore(encoder); |
| 274 | } |
| 275 | |
| 276 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) |
| 277 | crtc->funcs->restore(crtc); |
| 278 | |
| 279 | if (nv_two_heads(dev)) { |
| 280 | NV_INFO(dev, "Restoring CRTC_OWNER to %d.\n", |
| 281 | dev_priv->crtc_owner); |
| 282 | NVSetOwner(dev, dev_priv->crtc_owner); |
| 283 | } |
| 284 | |
| 285 | NVLockVgaCrtcs(dev, true); |
| 286 | } |
| 287 | |