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