1 /* 2 * Copyright 2014 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 * Authors: Ben Skeggs 23 */ 24 #include "outp.h" 25 #include "ior.h" 26 27 #include <subdev/bios.h> 28 #include <subdev/bios/dcb.h> 29 #include <subdev/i2c.h> 30 31 void 32 nvkm_outp_route(struct nvkm_disp *disp) 33 { 34 struct nvkm_outp *outp; 35 struct nvkm_ior *ior; 36 37 list_for_each_entry(ior, &disp->ior, head) { 38 if ((outp = ior->arm.outp) && ior->arm.outp != ior->asy.outp) { 39 OUTP_DBG(outp, "release %s", ior->name); 40 if (ior->func->route.set) 41 ior->func->route.set(outp, NULL); 42 ior->arm.outp = NULL; 43 } 44 } 45 46 list_for_each_entry(ior, &disp->ior, head) { 47 if ((outp = ior->asy.outp)) { 48 OUTP_DBG(outp, "acquire %s", ior->name); 49 if (ior->asy.outp != ior->arm.outp) { 50 if (ior->func->route.set) 51 ior->func->route.set(outp, ior); 52 ior->arm.outp = ior->asy.outp; 53 } 54 } 55 } 56 } 57 58 static enum nvkm_ior_proto 59 nvkm_outp_xlat(struct nvkm_outp *outp, enum nvkm_ior_type *type) 60 { 61 switch (outp->info.location) { 62 case 0: 63 switch (outp->info.type) { 64 case DCB_OUTPUT_ANALOG: *type = DAC; return CRT; 65 case DCB_OUTPUT_TV : *type = DAC; return TV; 66 case DCB_OUTPUT_TMDS : *type = SOR; return TMDS; 67 case DCB_OUTPUT_LVDS : *type = SOR; return LVDS; 68 case DCB_OUTPUT_DP : *type = SOR; return DP; 69 default: 70 break; 71 } 72 break; 73 case 1: 74 switch (outp->info.type) { 75 case DCB_OUTPUT_TMDS: *type = PIOR; return TMDS; 76 case DCB_OUTPUT_DP : *type = PIOR; return TMDS; /* not a bug */ 77 default: 78 break; 79 } 80 break; 81 default: 82 break; 83 } 84 WARN_ON(1); 85 return UNKNOWN; 86 } 87 88 void 89 nvkm_outp_release(struct nvkm_outp *outp, u8 user) 90 { 91 struct nvkm_ior *ior = outp->ior; 92 OUTP_TRACE(outp, "release %02x &= %02x %p", outp->acquired, ~user, ior); 93 if (ior) { 94 outp->acquired &= ~user; 95 if (!outp->acquired) { 96 if (outp->func->release && outp->ior) 97 outp->func->release(outp); 98 outp->ior->asy.outp = NULL; 99 outp->ior = NULL; 100 } 101 } 102 } 103 104 static inline int 105 nvkm_outp_acquire_ior(struct nvkm_outp *outp, u8 user, struct nvkm_ior *ior) 106 { 107 outp->ior = ior; 108 outp->ior->asy.outp = outp; 109 outp->ior->asy.link = outp->info.sorconf.link; 110 outp->acquired |= user; 111 return 0; 112 } 113 114 static inline int 115 nvkm_outp_acquire_hda(struct nvkm_outp *outp, enum nvkm_ior_type type, 116 u8 user, bool hda) 117 { 118 struct nvkm_ior *ior; 119 120 /* First preference is to reuse the OR that is currently armed 121 * on HW, if any, in order to prevent unnecessary switching. 122 */ 123 list_for_each_entry(ior, &outp->disp->ior, head) { 124 if (!ior->identity && !!ior->func->hda.hpd == hda && 125 !ior->asy.outp && ior->arm.outp == outp) 126 return nvkm_outp_acquire_ior(outp, user, ior); 127 } 128 129 /* Failing that, a completely unused OR is the next best thing. */ 130 list_for_each_entry(ior, &outp->disp->ior, head) { 131 if (!ior->identity && !!ior->func->hda.hpd == hda && 132 !ior->asy.outp && ior->type == type && !ior->arm.outp && 133 (ior->func->route.set || ior->id == __ffs(outp->info.or))) 134 return nvkm_outp_acquire_ior(outp, user, ior); 135 } 136 137 /* Last resort is to assign an OR that's already active on HW, 138 * but will be released during the next modeset. 139 */ 140 list_for_each_entry(ior, &outp->disp->ior, head) { 141 if (!ior->identity && !!ior->func->hda.hpd == hda && 142 !ior->asy.outp && ior->type == type && 143 (ior->func->route.set || ior->id == __ffs(outp->info.or))) 144 return nvkm_outp_acquire_ior(outp, user, ior); 145 } 146 147 return -ENOSPC; 148 } 149 150 int 151 nvkm_outp_acquire(struct nvkm_outp *outp, u8 user, bool hda) 152 { 153 struct nvkm_ior *ior = outp->ior; 154 enum nvkm_ior_proto proto; 155 enum nvkm_ior_type type; 156 157 OUTP_TRACE(outp, "acquire %02x |= %02x %p", outp->acquired, user, ior); 158 if (ior) { 159 outp->acquired |= user; 160 return 0; 161 } 162 163 /* Lookup a compatible, and unused, OR to assign to the device. */ 164 proto = nvkm_outp_xlat(outp, &type); 165 if (proto == UNKNOWN) 166 return -ENOSYS; 167 168 /* Deal with panels requiring identity-mapped SOR assignment. */ 169 if (outp->identity) { 170 ior = nvkm_ior_find(outp->disp, SOR, ffs(outp->info.or) - 1); 171 if (WARN_ON(!ior)) 172 return -ENOSPC; 173 return nvkm_outp_acquire_ior(outp, user, ior); 174 } 175 176 /* If we don't need HDA, first try to acquire an OR that doesn't 177 * support it to leave free the ones that do. 178 */ 179 if (!hda) { 180 if (!nvkm_outp_acquire_hda(outp, type, user, false)) 181 return 0; 182 183 /* Use a HDA-supporting SOR anyway. */ 184 return nvkm_outp_acquire_hda(outp, type, user, true); 185 } 186 187 /* We want HDA, try to acquire an OR that supports it. */ 188 if (!nvkm_outp_acquire_hda(outp, type, user, true)) 189 return 0; 190 191 /* There weren't any free ORs that support HDA, grab one that 192 * doesn't and at least allow display to work still. 193 */ 194 return nvkm_outp_acquire_hda(outp, type, user, false); 195 } 196 197 void 198 nvkm_outp_fini(struct nvkm_outp *outp) 199 { 200 if (outp->func->fini) 201 outp->func->fini(outp); 202 } 203 204 static void 205 nvkm_outp_init_route(struct nvkm_outp *outp) 206 { 207 struct nvkm_disp *disp = outp->disp; 208 enum nvkm_ior_proto proto; 209 enum nvkm_ior_type type; 210 struct nvkm_ior *ior; 211 int id, link; 212 213 /* Find any OR from the class that is able to support this device. */ 214 proto = nvkm_outp_xlat(outp, &type); 215 if (proto == UNKNOWN) 216 return; 217 218 ior = nvkm_ior_find(disp, type, -1); 219 if (!ior) { 220 WARN_ON(1); 221 return; 222 } 223 224 /* Determine the specific OR, if any, this device is attached to. */ 225 if (ior->func->route.get) { 226 id = ior->func->route.get(outp, &link); 227 if (id < 0) { 228 OUTP_DBG(outp, "no route"); 229 return; 230 } 231 } else { 232 /* Prior to DCB 4.1, this is hardwired like so. */ 233 id = ffs(outp->info.or) - 1; 234 link = (ior->type == SOR) ? outp->info.sorconf.link : 0; 235 } 236 237 ior = nvkm_ior_find(disp, type, id); 238 if (!ior) { 239 WARN_ON(1); 240 return; 241 } 242 243 /* Determine if the OR is already configured for this device. */ 244 ior->func->state(ior, &ior->arm); 245 if (!ior->arm.head || ior->arm.proto != proto) { 246 OUTP_DBG(outp, "no heads (%x %d %d)", ior->arm.head, 247 ior->arm.proto, proto); 248 return; 249 } 250 251 OUTP_DBG(outp, "on %s link %x", ior->name, ior->arm.link); 252 ior->arm.outp = outp; 253 } 254 255 void 256 nvkm_outp_init(struct nvkm_outp *outp) 257 { 258 nvkm_outp_init_route(outp); 259 if (outp->func->init) 260 outp->func->init(outp); 261 } 262 263 void 264 nvkm_outp_del(struct nvkm_outp **poutp) 265 { 266 struct nvkm_outp *outp = *poutp; 267 if (outp && !WARN_ON(!outp->func)) { 268 if (outp->func->dtor) 269 *poutp = outp->func->dtor(outp); 270 kfree(*poutp); 271 *poutp = NULL; 272 } 273 } 274 275 int 276 nvkm_outp_ctor(const struct nvkm_outp_func *func, struct nvkm_disp *disp, 277 int index, struct dcb_output *dcbE, struct nvkm_outp *outp) 278 { 279 struct nvkm_i2c *i2c = disp->engine.subdev.device->i2c; 280 enum nvkm_ior_proto proto; 281 enum nvkm_ior_type type; 282 283 outp->func = func; 284 outp->disp = disp; 285 outp->index = index; 286 outp->info = *dcbE; 287 outp->i2c = nvkm_i2c_bus_find(i2c, dcbE->i2c_index); 288 289 OUTP_DBG(outp, "type %02x loc %d or %d link %d con %x " 290 "edid %x bus %d head %x", 291 outp->info.type, outp->info.location, outp->info.or, 292 outp->info.type >= 2 ? outp->info.sorconf.link : 0, 293 outp->info.connector, outp->info.i2c_index, 294 outp->info.bus, outp->info.heads); 295 296 /* Cull output paths we can't map to an output resource. */ 297 proto = nvkm_outp_xlat(outp, &type); 298 if (proto == UNKNOWN) 299 return -ENODEV; 300 301 return 0; 302 } 303 304 static const struct nvkm_outp_func 305 nvkm_outp = { 306 }; 307 308 int 309 nvkm_outp_new(struct nvkm_disp *disp, int index, struct dcb_output *dcbE, 310 struct nvkm_outp **poutp) 311 { 312 if (!(*poutp = kzalloc(sizeof(**poutp), GFP_KERNEL))) 313 return -ENOMEM; 314 return nvkm_outp_ctor(&nvkm_outp, disp, index, dcbE, *poutp); 315 } 316