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(struct nvkm_udevice *udev, void *data, u32 size) 44 { 45 struct nvkm_object *object = &udev->object; 46 struct nvkm_device *device = udev->device; 47 struct nvkm_fb *fb = device->fb; 48 struct nvkm_instmem *imem = device->imem; 49 union { 50 struct nv_device_info_v0 v0; 51 } *args = data; 52 int ret = -ENOSYS; 53 54 nvif_ioctl(object, "device info size %d\n", size); 55 if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, false))) { 56 nvif_ioctl(object, "device info vers %d\n", args->v0.version); 57 } else 58 return ret; 59 60 switch (device->chipset) { 61 case 0x01a: 62 case 0x01f: 63 case 0x04c: 64 case 0x04e: 65 case 0x063: 66 case 0x067: 67 case 0x068: 68 case 0x0aa: 69 case 0x0ac: 70 case 0x0af: 71 args->v0.platform = NV_DEVICE_INFO_V0_IGP; 72 break; 73 default: 74 switch (device->type) { 75 case NVKM_DEVICE_PCI: 76 args->v0.platform = NV_DEVICE_INFO_V0_PCI; 77 break; 78 case NVKM_DEVICE_AGP: 79 args->v0.platform = NV_DEVICE_INFO_V0_AGP; 80 break; 81 case NVKM_DEVICE_PCIE: 82 args->v0.platform = NV_DEVICE_INFO_V0_PCIE; 83 break; 84 case NVKM_DEVICE_TEGRA: 85 args->v0.platform = NV_DEVICE_INFO_V0_SOC; 86 break; 87 default: 88 WARN_ON(1); 89 break; 90 } 91 break; 92 } 93 94 switch (device->card_type) { 95 case NV_04: args->v0.family = NV_DEVICE_INFO_V0_TNT; break; 96 case NV_10: 97 case NV_11: args->v0.family = NV_DEVICE_INFO_V0_CELSIUS; break; 98 case NV_20: args->v0.family = NV_DEVICE_INFO_V0_KELVIN; break; 99 case NV_30: args->v0.family = NV_DEVICE_INFO_V0_RANKINE; break; 100 case NV_40: args->v0.family = NV_DEVICE_INFO_V0_CURIE; break; 101 case NV_50: args->v0.family = NV_DEVICE_INFO_V0_TESLA; break; 102 case NV_C0: args->v0.family = NV_DEVICE_INFO_V0_FERMI; break; 103 case NV_E0: args->v0.family = NV_DEVICE_INFO_V0_KEPLER; break; 104 case GM100: args->v0.family = NV_DEVICE_INFO_V0_MAXWELL; break; 105 case GP100: args->v0.family = NV_DEVICE_INFO_V0_PASCAL; break; 106 default: 107 args->v0.family = 0; 108 break; 109 } 110 111 args->v0.chipset = device->chipset; 112 args->v0.revision = device->chiprev; 113 if (fb && fb->ram) 114 args->v0.ram_size = args->v0.ram_user = fb->ram->size; 115 else 116 args->v0.ram_size = args->v0.ram_user = 0; 117 if (imem && args->v0.ram_size > 0) 118 args->v0.ram_user = args->v0.ram_user - imem->reserved; 119 120 strncpy(args->v0.chip, device->chip->name, sizeof(args->v0.chip)); 121 strncpy(args->v0.name, device->name, sizeof(args->v0.name)); 122 return 0; 123 } 124 125 static int 126 nvkm_udevice_time(struct nvkm_udevice *udev, void *data, u32 size) 127 { 128 struct nvkm_object *object = &udev->object; 129 struct nvkm_device *device = udev->device; 130 union { 131 struct nv_device_time_v0 v0; 132 } *args = data; 133 int ret = -ENOSYS; 134 135 nvif_ioctl(object, "device time size %d\n", size); 136 if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, false))) { 137 nvif_ioctl(object, "device time vers %d\n", args->v0.version); 138 args->v0.time = nvkm_timer_read(device->timer); 139 } 140 141 return ret; 142 } 143 144 static int 145 nvkm_udevice_mthd(struct nvkm_object *object, u32 mthd, void *data, u32 size) 146 { 147 struct nvkm_udevice *udev = nvkm_udevice(object); 148 nvif_ioctl(object, "device mthd %08x\n", mthd); 149 switch (mthd) { 150 case NV_DEVICE_V0_INFO: 151 return nvkm_udevice_info(udev, data, size); 152 case NV_DEVICE_V0_TIME: 153 return nvkm_udevice_time(udev, data, size); 154 default: 155 break; 156 } 157 return -EINVAL; 158 } 159 160 static int 161 nvkm_udevice_rd08(struct nvkm_object *object, u64 addr, u8 *data) 162 { 163 struct nvkm_udevice *udev = nvkm_udevice(object); 164 *data = nvkm_rd08(udev->device, addr); 165 return 0; 166 } 167 168 static int 169 nvkm_udevice_rd16(struct nvkm_object *object, u64 addr, u16 *data) 170 { 171 struct nvkm_udevice *udev = nvkm_udevice(object); 172 *data = nvkm_rd16(udev->device, addr); 173 return 0; 174 } 175 176 static int 177 nvkm_udevice_rd32(struct nvkm_object *object, u64 addr, u32 *data) 178 { 179 struct nvkm_udevice *udev = nvkm_udevice(object); 180 *data = nvkm_rd32(udev->device, addr); 181 return 0; 182 } 183 184 static int 185 nvkm_udevice_wr08(struct nvkm_object *object, u64 addr, u8 data) 186 { 187 struct nvkm_udevice *udev = nvkm_udevice(object); 188 nvkm_wr08(udev->device, addr, data); 189 return 0; 190 } 191 192 static int 193 nvkm_udevice_wr16(struct nvkm_object *object, u64 addr, u16 data) 194 { 195 struct nvkm_udevice *udev = nvkm_udevice(object); 196 nvkm_wr16(udev->device, addr, data); 197 return 0; 198 } 199 200 static int 201 nvkm_udevice_wr32(struct nvkm_object *object, u64 addr, u32 data) 202 { 203 struct nvkm_udevice *udev = nvkm_udevice(object); 204 nvkm_wr32(udev->device, addr, data); 205 return 0; 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, 0); 216 *size = device->func->resource_size(device, 0); 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 (1ULL << NVKM_ENGINE_PM); 282 const struct nvkm_device_oclass *sclass = NULL; 283 int i; 284 285 for (; i = __ffs64(mask), mask && !sclass; mask &= ~(1ULL << i)) { 286 if (!(engine = nvkm_device_engine(device, i)) || 287 !(engine->func->base.sclass)) 288 continue; 289 oclass->engine = engine; 290 291 index -= engine->func->base.sclass(oclass, index, &sclass); 292 } 293 294 if (!sclass) { 295 switch (index) { 296 case 0: sclass = &nvkm_control_oclass; break; 297 case 1: 298 if (!device->mmu) 299 return -EINVAL; 300 sclass = &device->mmu->user; 301 break; 302 default: 303 return -EINVAL; 304 } 305 oclass->base = sclass->base; 306 } 307 308 oclass->ctor = nvkm_udevice_child_new; 309 oclass->priv = sclass; 310 return 0; 311 } 312 313 static const struct nvkm_object_func 314 nvkm_udevice_super = { 315 .init = nvkm_udevice_init, 316 .fini = nvkm_udevice_fini, 317 .mthd = nvkm_udevice_mthd, 318 .map = nvkm_udevice_map, 319 .rd08 = nvkm_udevice_rd08, 320 .rd16 = nvkm_udevice_rd16, 321 .rd32 = nvkm_udevice_rd32, 322 .wr08 = nvkm_udevice_wr08, 323 .wr16 = nvkm_udevice_wr16, 324 .wr32 = nvkm_udevice_wr32, 325 .sclass = nvkm_udevice_child_get, 326 }; 327 328 static const struct nvkm_object_func 329 nvkm_udevice = { 330 .init = nvkm_udevice_init, 331 .fini = nvkm_udevice_fini, 332 .mthd = nvkm_udevice_mthd, 333 .sclass = nvkm_udevice_child_get, 334 }; 335 336 static int 337 nvkm_udevice_new(const struct nvkm_oclass *oclass, void *data, u32 size, 338 struct nvkm_object **pobject) 339 { 340 union { 341 struct nv_device_v0 v0; 342 } *args = data; 343 struct nvkm_client *client = oclass->client; 344 struct nvkm_object *parent = &client->object; 345 const struct nvkm_object_func *func; 346 struct nvkm_udevice *udev; 347 int ret = -ENOSYS; 348 349 nvif_ioctl(parent, "create device size %d\n", size); 350 if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, false))) { 351 nvif_ioctl(parent, "create device v%d device %016llx\n", 352 args->v0.version, args->v0.device); 353 } else 354 return ret; 355 356 /* give priviledged clients register access */ 357 if (client->super) 358 func = &nvkm_udevice_super; 359 else 360 func = &nvkm_udevice; 361 362 if (!(udev = kzalloc(sizeof(*udev), GFP_KERNEL))) 363 return -ENOMEM; 364 nvkm_object_ctor(func, oclass, &udev->object); 365 *pobject = &udev->object; 366 367 /* find the device that matches what the client requested */ 368 if (args->v0.device != ~0) 369 udev->device = nvkm_device_find(args->v0.device); 370 else 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