1 /* 2 * Copyright 2012 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 #define nvkm_udevice(p) container_of((p), struct nvkm_udevice, object) 25 #include "priv.h" 26 #include "ctrl.h" 27 28 #include <core/client.h> 29 #include <subdev/fb.h> 30 #include <subdev/gsp.h> 31 #include <subdev/instmem.h> 32 #include <subdev/timer.h> 33 34 #include <nvif/class.h> 35 #include <nvif/cl0080.h> 36 #include <nvif/unpack.h> 37 38 struct nvkm_udevice { 39 struct nvkm_object object; 40 struct nvkm_device *device; 41 }; 42 43 static int 44 nvkm_udevice_info_subdev(struct nvkm_device *device, u64 mthd, u64 *data) 45 { 46 struct nvkm_subdev *subdev; 47 enum nvkm_subdev_type type; 48 49 switch (mthd & NV_DEVICE_INFO_UNIT) { 50 case NV_DEVICE_HOST(0): type = NVKM_ENGINE_FIFO; break; 51 default: 52 return -EINVAL; 53 } 54 55 subdev = nvkm_device_subdev(device, type, 0); 56 if (subdev) 57 return nvkm_subdev_info(subdev, mthd, data); 58 return -ENODEV; 59 } 60 61 static void 62 nvkm_udevice_info_v1(struct nvkm_device *device, 63 struct nv_device_info_v1_data *args) 64 { 65 if (args->mthd & NV_DEVICE_INFO_UNIT) { 66 if (nvkm_udevice_info_subdev(device, args->mthd, &args->data)) 67 args->mthd = NV_DEVICE_INFO_INVALID; 68 return; 69 } 70 args->mthd = NV_DEVICE_INFO_INVALID; 71 } 72 73 static int 74 nvkm_udevice_info(struct nvkm_udevice *udev, void *data, u32 size) 75 { 76 struct nvkm_object *object = &udev->object; 77 struct nvkm_device *device = udev->device; 78 struct nvkm_fb *fb = device->fb; 79 struct nvkm_instmem *imem = device->imem; 80 union { 81 struct nv_device_info_v0 v0; 82 struct nv_device_info_v1 v1; 83 } *args = data; 84 int ret = -ENOSYS, i; 85 86 nvif_ioctl(object, "device info size %d\n", size); 87 if (!(ret = nvif_unpack(ret, &data, &size, args->v1, 1, 1, true))) { 88 nvif_ioctl(object, "device info vers %d count %d\n", 89 args->v1.version, args->v1.count); 90 if (args->v1.count * sizeof(args->v1.data[0]) == size) { 91 for (i = 0; i < args->v1.count; i++) 92 nvkm_udevice_info_v1(device, &args->v1.data[i]); 93 return 0; 94 } 95 return -EINVAL; 96 } else 97 if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, false))) { 98 nvif_ioctl(object, "device info vers %d\n", args->v0.version); 99 } else 100 return ret; 101 102 switch (device->chipset) { 103 case 0x01a: 104 case 0x01f: 105 case 0x04c: 106 case 0x04e: 107 case 0x063: 108 case 0x067: 109 case 0x068: 110 case 0x0aa: 111 case 0x0ac: 112 case 0x0af: 113 args->v0.platform = NV_DEVICE_INFO_V0_IGP; 114 break; 115 default: 116 switch (device->type) { 117 case NVKM_DEVICE_PCI: 118 args->v0.platform = NV_DEVICE_INFO_V0_PCI; 119 break; 120 case NVKM_DEVICE_AGP: 121 args->v0.platform = NV_DEVICE_INFO_V0_AGP; 122 break; 123 case NVKM_DEVICE_PCIE: 124 args->v0.platform = NV_DEVICE_INFO_V0_PCIE; 125 break; 126 case NVKM_DEVICE_TEGRA: 127 args->v0.platform = NV_DEVICE_INFO_V0_SOC; 128 break; 129 default: 130 WARN_ON(1); 131 break; 132 } 133 break; 134 } 135 136 switch (device->card_type) { 137 case NV_04: args->v0.family = NV_DEVICE_INFO_V0_TNT; break; 138 case NV_10: 139 case NV_11: args->v0.family = NV_DEVICE_INFO_V0_CELSIUS; break; 140 case NV_20: args->v0.family = NV_DEVICE_INFO_V0_KELVIN; break; 141 case NV_30: args->v0.family = NV_DEVICE_INFO_V0_RANKINE; break; 142 case NV_40: args->v0.family = NV_DEVICE_INFO_V0_CURIE; break; 143 case NV_50: args->v0.family = NV_DEVICE_INFO_V0_TESLA; break; 144 case NV_C0: args->v0.family = NV_DEVICE_INFO_V0_FERMI; break; 145 case NV_E0: args->v0.family = NV_DEVICE_INFO_V0_KEPLER; break; 146 case GM100: args->v0.family = NV_DEVICE_INFO_V0_MAXWELL; break; 147 case GP100: args->v0.family = NV_DEVICE_INFO_V0_PASCAL; break; 148 case GV100: args->v0.family = NV_DEVICE_INFO_V0_VOLTA; break; 149 case TU100: args->v0.family = NV_DEVICE_INFO_V0_TURING; break; 150 case GA100: args->v0.family = NV_DEVICE_INFO_V0_AMPERE; break; 151 case AD100: args->v0.family = NV_DEVICE_INFO_V0_ADA; break; 152 case GH100: args->v0.family = NV_DEVICE_INFO_V0_HOPPER; break; 153 case GB10x: args->v0.family = NV_DEVICE_INFO_V0_BLACKWELL; break; 154 case GB20x: args->v0.family = NV_DEVICE_INFO_V0_BLACKWELL; break; 155 default: 156 args->v0.family = 0; 157 break; 158 } 159 160 args->v0.chipset = device->chipset; 161 args->v0.revision = device->chiprev; 162 if (fb && fb->ram) 163 args->v0.ram_size = args->v0.ram_user = fb->ram->size; 164 else 165 args->v0.ram_size = args->v0.ram_user = 0; 166 if (imem && args->v0.ram_size > 0) 167 args->v0.ram_user = args->v0.ram_user - imem->reserved; 168 169 snprintf(args->v0.chip, sizeof(args->v0.chip), "%s", device->chip->name); 170 snprintf(args->v0.name, sizeof(args->v0.name), "%s", device->name); 171 return 0; 172 } 173 174 static int 175 nvkm_udevice_time(struct nvkm_udevice *udev, void *data, u32 size) 176 { 177 struct nvkm_object *object = &udev->object; 178 struct nvkm_device *device = udev->device; 179 union { 180 struct nv_device_time_v0 v0; 181 } *args = data; 182 int ret = -ENOSYS; 183 184 nvif_ioctl(object, "device time size %d\n", size); 185 if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, false))) { 186 nvif_ioctl(object, "device time vers %d\n", args->v0.version); 187 args->v0.time = nvkm_timer_read(device->timer); 188 } 189 190 return ret; 191 } 192 193 static int 194 nvkm_udevice_gcx_ready(struct nvkm_udevice *udev, void *data, u32 size) 195 { 196 struct nvkm_object *object = &udev->object; 197 struct nvkm_device *device = udev->device; 198 struct nvkm_gsp *gsp = device->gsp; 199 union { 200 struct nv_device_gcx_ready_v0 v0; 201 } *args = data; 202 int ret = -ENOSYS; 203 204 if (!gsp) { 205 args->v0.ready = NV_DEVICE_GC6_READY | NV_DEVICE_GCOFF_READY; 206 return 0; 207 } 208 209 nvif_ioctl(object, "device gcx ready size %d\n", size); 210 ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, false); 211 if (!ret) { 212 nvif_ioctl(object, "device gcx ready vers %d\n", args->v0.version); 213 214 ret = nvkm_gsp_gcx_ready(gsp); 215 if (ret < 0) 216 return ret; 217 218 args->v0.ready = ret; 219 ret = 0; 220 } 221 222 return ret; 223 } 224 225 static int 226 nvkm_udevice_mthd(struct nvkm_object *object, u32 mthd, void *data, u32 size) 227 { 228 struct nvkm_udevice *udev = nvkm_udevice(object); 229 nvif_ioctl(object, "device mthd %08x\n", mthd); 230 switch (mthd) { 231 case NV_DEVICE_V0_INFO: 232 return nvkm_udevice_info(udev, data, size); 233 case NV_DEVICE_V0_TIME: 234 return nvkm_udevice_time(udev, data, size); 235 case NV_DEVICE_V0_GCX_READY: 236 return nvkm_udevice_gcx_ready(udev, data, size); 237 default: 238 break; 239 } 240 return -EINVAL; 241 } 242 243 static int 244 nvkm_udevice_map(struct nvkm_object *object, void *argv, u32 argc, 245 enum nvkm_object_map *type, u64 *addr, u64 *size) 246 { 247 struct nvkm_udevice *udev = nvkm_udevice(object); 248 struct nvkm_device *device = udev->device; 249 *type = NVKM_OBJECT_MAP_IO; 250 *addr = device->func->resource_addr(device, NVKM_BAR0_PRI); 251 *size = device->func->resource_size(device, NVKM_BAR0_PRI); 252 return 0; 253 } 254 255 static int 256 nvkm_udevice_fini(struct nvkm_object *object, enum nvkm_suspend_state suspend) 257 { 258 struct nvkm_udevice *udev = nvkm_udevice(object); 259 struct nvkm_device *device = udev->device; 260 int ret = 0; 261 262 mutex_lock(&device->mutex); 263 if (!--device->refcount) { 264 ret = nvkm_device_fini(device, suspend); 265 if (ret && suspend) { 266 device->refcount++; 267 goto done; 268 } 269 } 270 271 done: 272 mutex_unlock(&device->mutex); 273 return ret; 274 } 275 276 static int 277 nvkm_udevice_init(struct nvkm_object *object) 278 { 279 struct nvkm_udevice *udev = nvkm_udevice(object); 280 struct nvkm_device *device = udev->device; 281 int ret = 0; 282 283 mutex_lock(&device->mutex); 284 if (!device->refcount++) { 285 ret = nvkm_device_init(device); 286 if (ret) { 287 device->refcount--; 288 goto done; 289 } 290 } 291 292 done: 293 mutex_unlock(&device->mutex); 294 return ret; 295 } 296 297 static int 298 nvkm_udevice_child_new(const struct nvkm_oclass *oclass, 299 void *data, u32 size, struct nvkm_object **pobject) 300 { 301 struct nvkm_udevice *udev = nvkm_udevice(oclass->parent); 302 const struct nvkm_device_oclass *sclass = oclass->priv; 303 return sclass->ctor(udev->device, oclass, data, size, pobject); 304 } 305 306 static int 307 nvkm_udevice_child_get(struct nvkm_object *object, int index, 308 struct nvkm_oclass *oclass) 309 { 310 struct nvkm_udevice *udev = nvkm_udevice(object); 311 struct nvkm_device *device = udev->device; 312 struct nvkm_engine *engine; 313 u64 mask = (1ULL << NVKM_ENGINE_DMAOBJ) | 314 (1ULL << NVKM_ENGINE_FIFO) | 315 (1ULL << NVKM_ENGINE_DISP); 316 const struct nvkm_device_oclass *sclass = NULL; 317 int i; 318 319 for (; i = __ffs64(mask), mask && !sclass; mask &= ~(1ULL << i)) { 320 if (!(engine = nvkm_device_engine(device, i, 0)) || 321 !(engine->func->base.sclass)) 322 continue; 323 oclass->engine = engine; 324 325 index -= engine->func->base.sclass(oclass, index, &sclass); 326 } 327 328 if (!sclass) { 329 if (index-- == 0) 330 sclass = &nvkm_control_oclass; 331 else if (device->mmu && index-- == 0) 332 sclass = &device->mmu->user; 333 else if (device->fault && index-- == 0) 334 sclass = &device->fault->user; 335 else if (device->vfn && index-- == 0) 336 sclass = &device->vfn->user; 337 else 338 return -EINVAL; 339 340 oclass->base = sclass->base; 341 oclass->engine = NULL; 342 } 343 344 oclass->ctor = nvkm_udevice_child_new; 345 oclass->priv = sclass; 346 return 0; 347 } 348 349 static const struct nvkm_object_func 350 nvkm_udevice = { 351 .init = nvkm_udevice_init, 352 .fini = nvkm_udevice_fini, 353 .mthd = nvkm_udevice_mthd, 354 .map = nvkm_udevice_map, 355 .sclass = nvkm_udevice_child_get, 356 }; 357 358 static int 359 nvkm_udevice_new(const struct nvkm_oclass *oclass, void *data, u32 size, 360 struct nvkm_object **pobject) 361 { 362 struct nvkm_client *client = oclass->client; 363 struct nvkm_udevice *udev; 364 365 if (!(udev = kzalloc_obj(*udev))) 366 return -ENOMEM; 367 nvkm_object_ctor(&nvkm_udevice, oclass, &udev->object); 368 *pobject = &udev->object; 369 370 /* find the device that matches what the client requested */ 371 udev->device = nvkm_device_find(client->device); 372 if (!udev->device) 373 return -ENODEV; 374 375 return 0; 376 } 377 378 const struct nvkm_sclass 379 nvkm_udevice_sclass = { 380 .oclass = NV_DEVICE, 381 .minver = 0, 382 .maxver = 0, 383 .ctor = nvkm_udevice_new, 384 }; 385