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_TMDS : *type = SOR; return TMDS; 66 case DCB_OUTPUT_LVDS : *type = SOR; return LVDS; 67 case DCB_OUTPUT_DP : *type = SOR; return DP; 68 default: 69 break; 70 } 71 break; 72 case 1: 73 switch (outp->info.type) { 74 case DCB_OUTPUT_TMDS: *type = PIOR; return TMDS; 75 case DCB_OUTPUT_DP : *type = PIOR; return TMDS; /* not a bug */ 76 default: 77 break; 78 } 79 break; 80 default: 81 break; 82 } 83 WARN_ON(1); 84 return UNKNOWN; 85 } 86 87 void 88 nvkm_outp_release(struct nvkm_outp *outp, u8 user) 89 { 90 struct nvkm_ior *ior = outp->ior; 91 OUTP_TRACE(outp, "release %02x &= %02x %p", outp->acquired, ~user, ior); 92 if (ior) { 93 outp->acquired &= ~user; 94 if (!outp->acquired) { 95 outp->ior->asy.outp = NULL; 96 outp->ior = NULL; 97 } 98 } 99 } 100 101 static inline int 102 nvkm_outp_acquire_ior(struct nvkm_outp *outp, u8 user, struct nvkm_ior *ior) 103 { 104 outp->ior = ior; 105 outp->ior->asy.outp = outp; 106 outp->ior->asy.link = outp->info.sorconf.link; 107 outp->acquired |= user; 108 return 0; 109 } 110 111 int 112 nvkm_outp_acquire(struct nvkm_outp *outp, u8 user) 113 { 114 struct nvkm_ior *ior = outp->ior; 115 enum nvkm_ior_proto proto; 116 enum nvkm_ior_type type; 117 118 OUTP_TRACE(outp, "acquire %02x |= %02x %p", outp->acquired, user, ior); 119 if (ior) { 120 outp->acquired |= user; 121 return 0; 122 } 123 124 /* Lookup a compatible, and unused, OR to assign to the device. */ 125 proto = nvkm_outp_xlat(outp, &type); 126 if (proto == UNKNOWN) 127 return -ENOSYS; 128 129 /* First preference is to reuse the OR that is currently armed 130 * on HW, if any, in order to prevent unnecessary switching. 131 */ 132 list_for_each_entry(ior, &outp->disp->ior, head) { 133 if (!ior->asy.outp && ior->arm.outp == outp) 134 return nvkm_outp_acquire_ior(outp, user, ior); 135 } 136 137 /* Failing that, a completely unused OR is the next best thing. */ 138 list_for_each_entry(ior, &outp->disp->ior, head) { 139 if (!ior->asy.outp && ior->type == type && !ior->arm.outp && 140 (ior->func->route.set || ior->id == __ffs(outp->info.or))) 141 return nvkm_outp_acquire_ior(outp, user, ior); 142 } 143 144 /* Last resort is to assign an OR that's already active on HW, 145 * but will be released during the next modeset. 146 */ 147 list_for_each_entry(ior, &outp->disp->ior, head) { 148 if (!ior->asy.outp && ior->type == type && 149 (ior->func->route.set || ior->id == __ffs(outp->info.or))) 150 return nvkm_outp_acquire_ior(outp, user, ior); 151 } 152 153 return -ENOSPC; 154 } 155 156 void 157 nvkm_outp_fini(struct nvkm_outp *outp) 158 { 159 if (outp->func->fini) 160 outp->func->fini(outp); 161 } 162 163 static void 164 nvkm_outp_init_route(struct nvkm_outp *outp) 165 { 166 struct nvkm_disp *disp = outp->disp; 167 enum nvkm_ior_proto proto; 168 enum nvkm_ior_type type; 169 struct nvkm_ior *ior; 170 int id, link; 171 172 /* Find any OR from the class that is able to support this device. */ 173 proto = nvkm_outp_xlat(outp, &type); 174 if (proto == UNKNOWN) 175 return; 176 177 ior = nvkm_ior_find(disp, type, -1); 178 if (!ior) { 179 WARN_ON(1); 180 return; 181 } 182 183 /* Determine the specific OR, if any, this device is attached to. */ 184 if (ior->func->route.get) { 185 id = ior->func->route.get(outp, &link); 186 if (id < 0) { 187 OUTP_DBG(outp, "no route"); 188 return; 189 } 190 } else { 191 /* Prior to DCB 4.1, this is hardwired like so. */ 192 id = ffs(outp->info.or) - 1; 193 link = (ior->type == SOR) ? outp->info.sorconf.link : 0; 194 } 195 196 ior = nvkm_ior_find(disp, type, id); 197 if (!ior) { 198 WARN_ON(1); 199 return; 200 } 201 202 /* Determine if the OR is already configured for this device. */ 203 ior->func->state(ior, &ior->arm); 204 if (!ior->arm.head || ior->arm.proto != proto) { 205 OUTP_DBG(outp, "no heads (%x %d %d)", ior->arm.head, 206 ior->arm.proto, proto); 207 return; 208 } 209 210 OUTP_DBG(outp, "on %s link %x", ior->name, ior->arm.link); 211 ior->arm.outp = outp; 212 } 213 214 void 215 nvkm_outp_init(struct nvkm_outp *outp) 216 { 217 nvkm_outp_init_route(outp); 218 if (outp->func->init) 219 outp->func->init(outp); 220 } 221 222 void 223 nvkm_outp_del(struct nvkm_outp **poutp) 224 { 225 struct nvkm_outp *outp = *poutp; 226 if (outp && !WARN_ON(!outp->func)) { 227 if (outp->func->dtor) 228 *poutp = outp->func->dtor(outp); 229 kfree(*poutp); 230 *poutp = NULL; 231 } 232 } 233 234 int 235 nvkm_outp_ctor(const struct nvkm_outp_func *func, struct nvkm_disp *disp, 236 int index, struct dcb_output *dcbE, struct nvkm_outp *outp) 237 { 238 struct nvkm_i2c *i2c = disp->engine.subdev.device->i2c; 239 enum nvkm_ior_proto proto; 240 enum nvkm_ior_type type; 241 242 outp->func = func; 243 outp->disp = disp; 244 outp->index = index; 245 outp->info = *dcbE; 246 outp->i2c = nvkm_i2c_bus_find(i2c, dcbE->i2c_index); 247 outp->or = ffs(outp->info.or) - 1; 248 249 OUTP_DBG(outp, "type %02x loc %d or %d link %d con %x " 250 "edid %x bus %d head %x", 251 outp->info.type, outp->info.location, outp->info.or, 252 outp->info.type >= 2 ? outp->info.sorconf.link : 0, 253 outp->info.connector, outp->info.i2c_index, 254 outp->info.bus, outp->info.heads); 255 256 /* Cull output paths we can't map to an output resource. */ 257 proto = nvkm_outp_xlat(outp, &type); 258 if (proto == UNKNOWN) 259 return -ENODEV; 260 261 return 0; 262 } 263 264 static const struct nvkm_outp_func 265 nvkm_outp = { 266 }; 267 268 int 269 nvkm_outp_new(struct nvkm_disp *disp, int index, struct dcb_output *dcbE, 270 struct nvkm_outp **poutp) 271 { 272 if (!(*poutp = kzalloc(sizeof(**poutp), GFP_KERNEL))) 273 return -ENOMEM; 274 return nvkm_outp_ctor(&nvkm_outp, disp, index, dcbE, *poutp); 275 } 276