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: |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 84 | /* we need to ensure the heads are not tied henceforth, or reading any |
| 85 | * 8 bit reg on head B will fail |
| 86 | * setting a single arbitrary head solves that */ |
| 87 | NVSetOwner(dev, 0); |
| 88 | } |
| 89 | |
| 90 | int |
| 91 | nv04_display_create(struct drm_device *dev) |
| 92 | { |
| 93 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
Ben Skeggs | 04a39c5 | 2010-02-24 10:03:05 +1000 | [diff] [blame] | 94 | struct dcb_table *dcb = &dev_priv->vbios.dcb; |
Ben Skeggs | 8f1a608 | 2010-06-28 14:35:50 +1000 | [diff] [blame] | 95 | struct drm_connector *connector, *ct; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 96 | struct drm_encoder *encoder; |
| 97 | struct drm_crtc *crtc; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 98 | int i, ret; |
| 99 | |
Maarten Maathuis | ef2bb50 | 2009-12-13 16:53:12 +0100 | [diff] [blame] | 100 | NV_DEBUG_KMS(dev, "\n"); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 101 | |
| 102 | if (nv_two_heads(dev)) |
| 103 | nv04_display_store_initial_head_owner(dev); |
Francisco Jerez | 95f158e | 2009-12-11 23:44:49 +0100 | [diff] [blame] | 104 | nouveau_hw_save_vga_fonts(dev, 1); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 105 | |
| 106 | drm_mode_config_init(dev); |
| 107 | drm_mode_create_scaling_mode_property(dev); |
| 108 | drm_mode_create_dithering_property(dev); |
| 109 | |
| 110 | dev->mode_config.funcs = (void *)&nouveau_mode_config_funcs; |
| 111 | |
| 112 | dev->mode_config.min_width = 0; |
| 113 | dev->mode_config.min_height = 0; |
| 114 | switch (dev_priv->card_type) { |
| 115 | case NV_04: |
| 116 | dev->mode_config.max_width = 2048; |
| 117 | dev->mode_config.max_height = 2048; |
| 118 | break; |
| 119 | default: |
| 120 | dev->mode_config.max_width = 4096; |
| 121 | dev->mode_config.max_height = 4096; |
| 122 | break; |
| 123 | } |
| 124 | |
| 125 | dev->mode_config.fb_base = dev_priv->fb_phys; |
| 126 | |
| 127 | nv04_crtc_create(dev, 0); |
| 128 | if (nv_two_heads(dev)) |
| 129 | nv04_crtc_create(dev, 1); |
| 130 | |
| 131 | for (i = 0; i < dcb->entries; i++) { |
| 132 | struct dcb_entry *dcbent = &dcb->entry[i]; |
| 133 | |
Ben Skeggs | 8f1a608 | 2010-06-28 14:35:50 +1000 | [diff] [blame] | 134 | connector = nouveau_connector_create(dev, dcbent->connector); |
| 135 | if (IS_ERR(connector)) |
| 136 | continue; |
| 137 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 138 | switch (dcbent->type) { |
| 139 | case OUTPUT_ANALOG: |
Ben Skeggs | 8f1a608 | 2010-06-28 14:35:50 +1000 | [diff] [blame] | 140 | ret = nv04_dac_create(connector, dcbent); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 141 | break; |
| 142 | case OUTPUT_LVDS: |
| 143 | case OUTPUT_TMDS: |
Ben Skeggs | 8f1a608 | 2010-06-28 14:35:50 +1000 | [diff] [blame] | 144 | ret = nv04_dfp_create(connector, dcbent); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 145 | break; |
| 146 | case OUTPUT_TV: |
| 147 | if (dcbent->location == DCB_LOC_ON_CHIP) |
Ben Skeggs | 8f1a608 | 2010-06-28 14:35:50 +1000 | [diff] [blame] | 148 | ret = nv17_tv_create(connector, dcbent); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 149 | else |
Ben Skeggs | 8f1a608 | 2010-06-28 14:35:50 +1000 | [diff] [blame] | 150 | ret = nv04_tv_create(connector, dcbent); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 151 | break; |
| 152 | default: |
| 153 | NV_WARN(dev, "DCB type %d not known\n", dcbent->type); |
| 154 | continue; |
| 155 | } |
| 156 | |
| 157 | if (ret) |
| 158 | continue; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 159 | } |
| 160 | |
Ben Skeggs | 8f1a608 | 2010-06-28 14:35:50 +1000 | [diff] [blame] | 161 | list_for_each_entry_safe(connector, ct, |
| 162 | &dev->mode_config.connector_list, head) { |
| 163 | if (!connector->encoder_ids[0]) { |
| 164 | NV_WARN(dev, "%s has no encoders, removing\n", |
| 165 | drm_get_connector_name(connector)); |
| 166 | connector->funcs->destroy(connector); |
| 167 | } |
| 168 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 169 | |
| 170 | /* Save previous state */ |
| 171 | NVLockVgaCrtcs(dev, false); |
| 172 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 173 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) |
| 174 | crtc->funcs->save(crtc); |
| 175 | |
| 176 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { |
| 177 | struct drm_encoder_helper_funcs *func = encoder->helper_private; |
| 178 | |
| 179 | func->save(encoder); |
| 180 | } |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | void |
| 186 | nv04_display_destroy(struct drm_device *dev) |
| 187 | { |
| 188 | struct drm_encoder *encoder; |
| 189 | struct drm_crtc *crtc; |
| 190 | |
Maarten Maathuis | ef2bb50 | 2009-12-13 16:53:12 +0100 | [diff] [blame] | 191 | NV_DEBUG_KMS(dev, "\n"); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 192 | |
| 193 | /* Turn every CRTC off. */ |
| 194 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { |
| 195 | struct drm_mode_set modeset = { |
| 196 | .crtc = crtc, |
| 197 | }; |
| 198 | |
| 199 | crtc->funcs->set_config(&modeset); |
| 200 | } |
| 201 | |
| 202 | /* Restore state */ |
| 203 | NVLockVgaCrtcs(dev, false); |
| 204 | |
| 205 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { |
| 206 | struct drm_encoder_helper_funcs *func = encoder->helper_private; |
| 207 | |
| 208 | func->restore(encoder); |
| 209 | } |
| 210 | |
| 211 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) |
| 212 | crtc->funcs->restore(crtc); |
| 213 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 214 | drm_mode_config_cleanup(dev); |
Francisco Jerez | 95f158e | 2009-12-11 23:44:49 +0100 | [diff] [blame] | 215 | |
| 216 | nouveau_hw_save_vga_fonts(dev, 0); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | void |
| 220 | nv04_display_restore(struct drm_device *dev) |
| 221 | { |
| 222 | struct drm_nouveau_private *dev_priv = dev->dev_private; |
| 223 | struct drm_encoder *encoder; |
| 224 | struct drm_crtc *crtc; |
| 225 | |
| 226 | NVLockVgaCrtcs(dev, false); |
| 227 | |
| 228 | /* meh.. modeset apparently doesn't setup all the regs and depends |
| 229 | * on pre-existing state, for now load the state of the card *before* |
| 230 | * nouveau was loaded, and then do a modeset. |
| 231 | * |
| 232 | * best thing to do probably is to make save/restore routines not |
| 233 | * save/restore "pre-load" state, but more general so we can save |
| 234 | * on suspend too. |
| 235 | */ |
| 236 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { |
| 237 | struct drm_encoder_helper_funcs *func = encoder->helper_private; |
| 238 | |
| 239 | func->restore(encoder); |
| 240 | } |
| 241 | |
| 242 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) |
| 243 | crtc->funcs->restore(crtc); |
| 244 | |
Francisco Jerez | d06ab84 | 2010-07-20 02:33:16 +0200 | [diff] [blame^] | 245 | if (nv_two_heads(dev)) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 246 | NVSetOwner(dev, dev_priv->crtc_owner); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 247 | |
| 248 | NVLockVgaCrtcs(dev, true); |
| 249 | } |
| 250 | |