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