Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 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 | |
| 25 | #include <core/object.h> |
| 26 | #include <core/device.h> |
| 27 | #include <core/client.h> |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 28 | #include <core/option.h> |
Ben Skeggs | d01c309 | 2014-08-10 04:10:21 +1000 | [diff] [blame] | 29 | #include <nvif/unpack.h> |
| 30 | #include <nvif/class.h> |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 31 | #include <core/class.h> |
| 32 | |
Ben Skeggs | d01c309 | 2014-08-10 04:10:21 +1000 | [diff] [blame] | 33 | #include <subdev/fb.h> |
| 34 | #include <subdev/instmem.h> |
| 35 | |
Ben Skeggs | 9838366 | 2013-10-17 09:56:02 +1000 | [diff] [blame] | 36 | #include "priv.h" |
Ben Skeggs | ed76a87 | 2014-06-13 12:42:21 +1000 | [diff] [blame] | 37 | #include "acpi.h" |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 38 | |
| 39 | static DEFINE_MUTEX(nv_devices_mutex); |
| 40 | static LIST_HEAD(nv_devices); |
| 41 | |
| 42 | struct nouveau_device * |
| 43 | nouveau_device_find(u64 name) |
| 44 | { |
| 45 | struct nouveau_device *device, *match = NULL; |
| 46 | mutex_lock(&nv_devices_mutex); |
| 47 | list_for_each_entry(device, &nv_devices, head) { |
| 48 | if (device->handle == name) { |
| 49 | match = device; |
| 50 | break; |
| 51 | } |
| 52 | } |
| 53 | mutex_unlock(&nv_devices_mutex); |
| 54 | return match; |
| 55 | } |
| 56 | |
Ben Skeggs | 803c178 | 2014-08-10 04:10:21 +1000 | [diff] [blame^] | 57 | int |
| 58 | nouveau_device_list(u64 *name, int size) |
| 59 | { |
| 60 | struct nouveau_device *device; |
| 61 | int nr = 0; |
| 62 | mutex_lock(&nv_devices_mutex); |
| 63 | list_for_each_entry(device, &nv_devices, head) { |
| 64 | if (nr++ < size) |
| 65 | name[nr - 1] = device->handle; |
| 66 | } |
| 67 | mutex_unlock(&nv_devices_mutex); |
| 68 | return nr; |
| 69 | } |
| 70 | |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 71 | /****************************************************************************** |
| 72 | * nouveau_devobj (0x0080): class implementation |
| 73 | *****************************************************************************/ |
Ben Skeggs | d01c309 | 2014-08-10 04:10:21 +1000 | [diff] [blame] | 74 | |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 75 | struct nouveau_devobj { |
| 76 | struct nouveau_parent base; |
| 77 | struct nouveau_object *subdev[NVDEV_SUBDEV_NR]; |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 78 | }; |
| 79 | |
Ben Skeggs | d01c309 | 2014-08-10 04:10:21 +1000 | [diff] [blame] | 80 | static int |
| 81 | nouveau_devobj_info(struct nouveau_object *object, void *data, u32 size) |
| 82 | { |
| 83 | struct nouveau_device *device = nv_device(object); |
| 84 | struct nouveau_fb *pfb = nouveau_fb(device); |
| 85 | struct nouveau_instmem *imem = nouveau_instmem(device); |
| 86 | union { |
| 87 | struct nv_device_info_v0 v0; |
| 88 | } *args = data; |
| 89 | int ret; |
| 90 | |
| 91 | nv_ioctl(object, "device info size %d\n", size); |
| 92 | if (nvif_unpack(args->v0, 0, 0, false)) { |
| 93 | nv_ioctl(object, "device info vers %d\n", args->v0.version); |
| 94 | } else |
| 95 | return ret; |
| 96 | |
| 97 | switch (device->chipset) { |
| 98 | case 0x01a: |
| 99 | case 0x01f: |
| 100 | case 0x04c: |
| 101 | case 0x04e: |
| 102 | case 0x063: |
| 103 | case 0x067: |
| 104 | case 0x068: |
| 105 | case 0x0aa: |
| 106 | case 0x0ac: |
| 107 | case 0x0af: |
| 108 | args->v0.platform = NV_DEVICE_INFO_V0_IGP; |
| 109 | break; |
| 110 | default: |
| 111 | if (device->pdev) { |
| 112 | if (pci_find_capability(device->pdev, PCI_CAP_ID_AGP)) |
| 113 | args->v0.platform = NV_DEVICE_INFO_V0_AGP; |
| 114 | else |
| 115 | if (pci_is_pcie(device->pdev)) |
| 116 | args->v0.platform = NV_DEVICE_INFO_V0_PCIE; |
| 117 | else |
| 118 | args->v0.platform = NV_DEVICE_INFO_V0_PCI; |
| 119 | } else { |
| 120 | args->v0.platform = NV_DEVICE_INFO_V0_SOC; |
| 121 | } |
| 122 | break; |
| 123 | } |
| 124 | |
| 125 | switch (device->card_type) { |
| 126 | case NV_04: args->v0.family = NV_DEVICE_INFO_V0_TNT; break; |
| 127 | case NV_10: |
| 128 | case NV_11: args->v0.family = NV_DEVICE_INFO_V0_CELSIUS; break; |
| 129 | case NV_20: args->v0.family = NV_DEVICE_INFO_V0_KELVIN; break; |
| 130 | case NV_30: args->v0.family = NV_DEVICE_INFO_V0_RANKINE; break; |
| 131 | case NV_40: args->v0.family = NV_DEVICE_INFO_V0_CURIE; break; |
| 132 | case NV_50: args->v0.family = NV_DEVICE_INFO_V0_TESLA; break; |
Ben Skeggs | 9c210f3 | 2014-08-10 04:10:21 +1000 | [diff] [blame] | 133 | case NV_C0: args->v0.family = NV_DEVICE_INFO_V0_FERMI; break; |
Ben Skeggs | d01c309 | 2014-08-10 04:10:21 +1000 | [diff] [blame] | 134 | case NV_E0: args->v0.family = NV_DEVICE_INFO_V0_KEPLER; break; |
| 135 | case GM100: args->v0.family = NV_DEVICE_INFO_V0_MAXWELL; break; |
| 136 | default: |
| 137 | args->v0.family = 0; |
| 138 | break; |
| 139 | } |
| 140 | |
| 141 | args->v0.chipset = device->chipset; |
| 142 | args->v0.revision = device->chipset >= 0x10 ? nv_rd32(device, 0) : 0x00; |
| 143 | if (pfb) args->v0.ram_size = args->v0.ram_user = pfb->ram->size; |
| 144 | else args->v0.ram_size = args->v0.ram_user = 0; |
| 145 | if (imem) args->v0.ram_user = args->v0.ram_user - imem->reserved; |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | static int |
| 150 | nouveau_devobj_mthd(struct nouveau_object *object, u32 mthd, |
| 151 | void *data, u32 size) |
| 152 | { |
| 153 | switch (mthd) { |
| 154 | case NV_DEVICE_V0_INFO: |
| 155 | return nouveau_devobj_info(object, data, size); |
| 156 | default: |
| 157 | break; |
| 158 | } |
| 159 | return -EINVAL; |
| 160 | } |
| 161 | |
| 162 | static u8 |
| 163 | nouveau_devobj_rd08(struct nouveau_object *object, u64 addr) |
| 164 | { |
| 165 | return nv_rd08(object->engine, addr); |
| 166 | } |
| 167 | |
| 168 | static u16 |
| 169 | nouveau_devobj_rd16(struct nouveau_object *object, u64 addr) |
| 170 | { |
| 171 | return nv_rd16(object->engine, addr); |
| 172 | } |
| 173 | |
| 174 | static u32 |
| 175 | nouveau_devobj_rd32(struct nouveau_object *object, u64 addr) |
| 176 | { |
| 177 | return nv_rd32(object->engine, addr); |
| 178 | } |
| 179 | |
| 180 | static void |
| 181 | nouveau_devobj_wr08(struct nouveau_object *object, u64 addr, u8 data) |
| 182 | { |
| 183 | nv_wr08(object->engine, addr, data); |
| 184 | } |
| 185 | |
| 186 | static void |
| 187 | nouveau_devobj_wr16(struct nouveau_object *object, u64 addr, u16 data) |
| 188 | { |
| 189 | nv_wr16(object->engine, addr, data); |
| 190 | } |
| 191 | |
| 192 | static void |
| 193 | nouveau_devobj_wr32(struct nouveau_object *object, u64 addr, u32 data) |
| 194 | { |
| 195 | nv_wr32(object->engine, addr, data); |
| 196 | } |
| 197 | |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 198 | static const u64 disable_map[] = { |
| 199 | [NVDEV_SUBDEV_VBIOS] = NV_DEVICE_DISABLE_VBIOS, |
Ben Skeggs | 206c38a | 2012-11-01 11:09:53 +1000 | [diff] [blame] | 200 | [NVDEV_SUBDEV_DEVINIT] = NV_DEVICE_DISABLE_CORE, |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 201 | [NVDEV_SUBDEV_GPIO] = NV_DEVICE_DISABLE_CORE, |
| 202 | [NVDEV_SUBDEV_I2C] = NV_DEVICE_DISABLE_CORE, |
Ben Skeggs | 206c38a | 2012-11-01 11:09:53 +1000 | [diff] [blame] | 203 | [NVDEV_SUBDEV_CLOCK] = NV_DEVICE_DISABLE_CORE, |
| 204 | [NVDEV_SUBDEV_MXM] = NV_DEVICE_DISABLE_CORE, |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 205 | [NVDEV_SUBDEV_MC] = NV_DEVICE_DISABLE_CORE, |
Martin Peres | a10220b | 2012-11-04 01:01:53 +0100 | [diff] [blame] | 206 | [NVDEV_SUBDEV_BUS] = NV_DEVICE_DISABLE_CORE, |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 207 | [NVDEV_SUBDEV_TIMER] = NV_DEVICE_DISABLE_CORE, |
| 208 | [NVDEV_SUBDEV_FB] = NV_DEVICE_DISABLE_CORE, |
Ben Skeggs | 206c38a | 2012-11-01 11:09:53 +1000 | [diff] [blame] | 209 | [NVDEV_SUBDEV_LTCG] = NV_DEVICE_DISABLE_CORE, |
| 210 | [NVDEV_SUBDEV_IBUS] = NV_DEVICE_DISABLE_CORE, |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 211 | [NVDEV_SUBDEV_INSTMEM] = NV_DEVICE_DISABLE_CORE, |
Ben Skeggs | 206c38a | 2012-11-01 11:09:53 +1000 | [diff] [blame] | 212 | [NVDEV_SUBDEV_VM] = NV_DEVICE_DISABLE_CORE, |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 213 | [NVDEV_SUBDEV_BAR] = NV_DEVICE_DISABLE_CORE, |
| 214 | [NVDEV_SUBDEV_VOLT] = NV_DEVICE_DISABLE_CORE, |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 215 | [NVDEV_SUBDEV_THERM] = NV_DEVICE_DISABLE_CORE, |
Ben Skeggs | ff4b42c | 2013-10-15 09:38:12 +1000 | [diff] [blame] | 216 | [NVDEV_SUBDEV_PWR] = NV_DEVICE_DISABLE_CORE, |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 217 | [NVDEV_ENGINE_DMAOBJ] = NV_DEVICE_DISABLE_CORE, |
Ben Skeggs | aa4d7a4 | 2013-02-13 15:29:11 +1000 | [diff] [blame] | 218 | [NVDEV_ENGINE_PERFMON] = NV_DEVICE_DISABLE_CORE, |
Ben Skeggs | 206c38a | 2012-11-01 11:09:53 +1000 | [diff] [blame] | 219 | [NVDEV_ENGINE_FIFO] = NV_DEVICE_DISABLE_FIFO, |
| 220 | [NVDEV_ENGINE_SW] = NV_DEVICE_DISABLE_FIFO, |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 221 | [NVDEV_ENGINE_GR] = NV_DEVICE_DISABLE_GRAPH, |
| 222 | [NVDEV_ENGINE_MPEG] = NV_DEVICE_DISABLE_MPEG, |
| 223 | [NVDEV_ENGINE_ME] = NV_DEVICE_DISABLE_ME, |
| 224 | [NVDEV_ENGINE_VP] = NV_DEVICE_DISABLE_VP, |
| 225 | [NVDEV_ENGINE_CRYPT] = NV_DEVICE_DISABLE_CRYPT, |
| 226 | [NVDEV_ENGINE_BSP] = NV_DEVICE_DISABLE_BSP, |
| 227 | [NVDEV_ENGINE_PPP] = NV_DEVICE_DISABLE_PPP, |
| 228 | [NVDEV_ENGINE_COPY0] = NV_DEVICE_DISABLE_COPY0, |
| 229 | [NVDEV_ENGINE_COPY1] = NV_DEVICE_DISABLE_COPY1, |
Ben Skeggs | c42a7ae | 2013-08-21 13:26:42 +1000 | [diff] [blame] | 230 | [NVDEV_ENGINE_VIC] = NV_DEVICE_DISABLE_VIC, |
Ben Skeggs | 206c38a | 2012-11-01 11:09:53 +1000 | [diff] [blame] | 231 | [NVDEV_ENGINE_VENC] = NV_DEVICE_DISABLE_VENC, |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 232 | [NVDEV_ENGINE_DISP] = NV_DEVICE_DISABLE_DISP, |
| 233 | [NVDEV_SUBDEV_NR] = 0, |
| 234 | }; |
| 235 | |
| 236 | static int |
| 237 | nouveau_devobj_ctor(struct nouveau_object *parent, |
| 238 | struct nouveau_object *engine, |
| 239 | struct nouveau_oclass *oclass, void *data, u32 size, |
| 240 | struct nouveau_object **pobject) |
| 241 | { |
| 242 | struct nouveau_client *client = nv_client(parent); |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 243 | struct nouveau_device *device; |
| 244 | struct nouveau_devobj *devobj; |
| 245 | struct nv_device_class *args = data; |
Marcin Slusarz | 950fbfa | 2012-12-29 16:24:37 +0100 | [diff] [blame] | 246 | u32 boot0, strap; |
| 247 | u64 disable, mmio_base, mmio_size; |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 248 | void __iomem *map; |
Ben Skeggs | 7234d02 | 2012-10-02 10:30:34 +1000 | [diff] [blame] | 249 | int ret, i, c; |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 250 | |
| 251 | if (size < sizeof(struct nv_device_class)) |
| 252 | return -EINVAL; |
| 253 | |
| 254 | /* find the device subdev that matches what the client requested */ |
| 255 | device = nv_device(client->device); |
| 256 | if (args->device != ~0) { |
| 257 | device = nouveau_device_find(args->device); |
| 258 | if (!device) |
| 259 | return -ENODEV; |
| 260 | } |
| 261 | |
Ben Skeggs | 9838366 | 2013-10-17 09:56:02 +1000 | [diff] [blame] | 262 | ret = nouveau_parent_create(parent, nv_object(device), oclass, 0, |
| 263 | nouveau_control_oclass, |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 264 | (1ULL << NVDEV_ENGINE_DMAOBJ) | |
| 265 | (1ULL << NVDEV_ENGINE_FIFO) | |
Ben Skeggs | aa4d7a4 | 2013-02-13 15:29:11 +1000 | [diff] [blame] | 266 | (1ULL << NVDEV_ENGINE_DISP) | |
| 267 | (1ULL << NVDEV_ENGINE_PERFMON), &devobj); |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 268 | *pobject = nv_object(devobj); |
| 269 | if (ret) |
| 270 | return ret; |
| 271 | |
Alexandre Courbot | 420b946 | 2014-02-17 15:17:26 +0900 | [diff] [blame] | 272 | mmio_base = nv_device_resource_start(device, 0); |
| 273 | mmio_size = nv_device_resource_len(device, 0); |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 274 | |
| 275 | /* translate api disable mask into internal mapping */ |
| 276 | disable = args->debug0; |
| 277 | for (i = 0; i < NVDEV_SUBDEV_NR; i++) { |
| 278 | if (args->disable & disable_map[i]) |
| 279 | disable |= (1ULL << i); |
| 280 | } |
| 281 | |
| 282 | /* identify the chipset, and determine classes of subdev/engines */ |
| 283 | if (!(args->disable & NV_DEVICE_DISABLE_IDENTIFY) && |
| 284 | !device->card_type) { |
| 285 | map = ioremap(mmio_base, 0x102000); |
Ben Skeggs | 43b1e9c | 2012-08-06 16:31:26 +1000 | [diff] [blame] | 286 | if (map == NULL) |
| 287 | return -ENOMEM; |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 288 | |
| 289 | /* switch mmio to cpu's native endianness */ |
| 290 | #ifndef __BIG_ENDIAN |
| 291 | if (ioread32_native(map + 0x000004) != 0x00000000) |
| 292 | #else |
| 293 | if (ioread32_native(map + 0x000004) == 0x00000000) |
| 294 | #endif |
| 295 | iowrite32_native(0x01000001, map + 0x000004); |
| 296 | |
| 297 | /* read boot0 and strapping information */ |
| 298 | boot0 = ioread32_native(map + 0x000000); |
| 299 | strap = ioread32_native(map + 0x101000); |
| 300 | iounmap(map); |
| 301 | |
| 302 | /* determine chipset and derive architecture from it */ |
Ben Skeggs | dd5b84a | 2013-09-28 07:31:07 +1000 | [diff] [blame] | 303 | if ((boot0 & 0x1f000000) > 0) { |
| 304 | device->chipset = (boot0 & 0x1ff00000) >> 20; |
| 305 | switch (device->chipset & 0x1f0) { |
Ben Skeggs | aabf19c | 2013-11-05 13:14:25 +1000 | [diff] [blame] | 306 | case 0x010: { |
Ilia Mirkin | 4a0ff75 | 2013-09-05 04:45:02 -0400 | [diff] [blame] | 307 | if (0x461 & (1 << (device->chipset & 0xf))) |
| 308 | device->card_type = NV_10; |
| 309 | else |
| 310 | device->card_type = NV_11; |
| 311 | break; |
| 312 | } |
Ben Skeggs | aabf19c | 2013-11-05 13:14:25 +1000 | [diff] [blame] | 313 | case 0x020: device->card_type = NV_20; break; |
| 314 | case 0x030: device->card_type = NV_30; break; |
| 315 | case 0x040: |
| 316 | case 0x060: device->card_type = NV_40; break; |
| 317 | case 0x050: |
| 318 | case 0x080: |
| 319 | case 0x090: |
| 320 | case 0x0a0: device->card_type = NV_50; break; |
Ben Skeggs | 9c210f3 | 2014-08-10 04:10:21 +1000 | [diff] [blame] | 321 | case 0x0c0: |
| 322 | case 0x0d0: device->card_type = NV_C0; break; |
Ben Skeggs | aabf19c | 2013-11-05 13:14:25 +1000 | [diff] [blame] | 323 | case 0x0e0: |
| 324 | case 0x0f0: |
| 325 | case 0x100: device->card_type = NV_E0; break; |
Ben Skeggs | 3f20464 | 2014-02-24 11:28:37 +1000 | [diff] [blame] | 326 | case 0x110: device->card_type = GM100; break; |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 327 | default: |
| 328 | break; |
| 329 | } |
| 330 | } else |
| 331 | if ((boot0 & 0xff00fff0) == 0x20004000) { |
| 332 | if (boot0 & 0x00f00000) |
| 333 | device->chipset = 0x05; |
| 334 | else |
| 335 | device->chipset = 0x04; |
| 336 | device->card_type = NV_04; |
| 337 | } |
| 338 | |
| 339 | switch (device->card_type) { |
| 340 | case NV_04: ret = nv04_identify(device); break; |
Ilia Mirkin | 4a0ff75 | 2013-09-05 04:45:02 -0400 | [diff] [blame] | 341 | case NV_10: |
| 342 | case NV_11: ret = nv10_identify(device); break; |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 343 | case NV_20: ret = nv20_identify(device); break; |
| 344 | case NV_30: ret = nv30_identify(device); break; |
| 345 | case NV_40: ret = nv40_identify(device); break; |
| 346 | case NV_50: ret = nv50_identify(device); break; |
Ben Skeggs | 9c210f3 | 2014-08-10 04:10:21 +1000 | [diff] [blame] | 347 | case NV_C0: ret = nvc0_identify(device); break; |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 348 | case NV_E0: ret = nve0_identify(device); break; |
Ben Skeggs | 3f20464 | 2014-02-24 11:28:37 +1000 | [diff] [blame] | 349 | case GM100: ret = gm100_identify(device); break; |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 350 | default: |
| 351 | ret = -EINVAL; |
| 352 | break; |
| 353 | } |
| 354 | |
| 355 | if (ret) { |
| 356 | nv_error(device, "unknown chipset, 0x%08x\n", boot0); |
| 357 | return ret; |
| 358 | } |
| 359 | |
| 360 | nv_info(device, "BOOT0 : 0x%08x\n", boot0); |
Ben Skeggs | 2094dd8 | 2012-07-27 08:28:20 +1000 | [diff] [blame] | 361 | nv_info(device, "Chipset: %s (NV%02X)\n", |
| 362 | device->cname, device->chipset); |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 363 | nv_info(device, "Family : NV%02X\n", device->card_type); |
| 364 | |
| 365 | /* determine frequency of timing crystal */ |
Ilia Mirkin | 8aa816b | 2013-09-05 04:45:03 -0400 | [diff] [blame] | 366 | if ( device->card_type <= NV_10 || device->chipset < 0x17 || |
Viktor Novotný | 1f2285d4 | 2012-11-10 19:24:06 +0100 | [diff] [blame] | 367 | (device->chipset >= 0x20 && device->chipset < 0x25)) |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 368 | strap &= 0x00000040; |
| 369 | else |
| 370 | strap &= 0x00400040; |
| 371 | |
| 372 | switch (strap) { |
| 373 | case 0x00000000: device->crystal = 13500; break; |
| 374 | case 0x00000040: device->crystal = 14318; break; |
| 375 | case 0x00400000: device->crystal = 27000; break; |
| 376 | case 0x00400040: device->crystal = 25000; break; |
| 377 | } |
| 378 | |
| 379 | nv_debug(device, "crystal freq: %dKHz\n", device->crystal); |
| 380 | } |
| 381 | |
| 382 | if (!(args->disable & NV_DEVICE_DISABLE_MMIO) && |
| 383 | !nv_subdev(device)->mmio) { |
| 384 | nv_subdev(device)->mmio = ioremap(mmio_base, mmio_size); |
| 385 | if (!nv_subdev(device)->mmio) { |
| 386 | nv_error(device, "unable to map device registers\n"); |
Ben Skeggs | 43b1e9c | 2012-08-06 16:31:26 +1000 | [diff] [blame] | 387 | return -ENOMEM; |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
| 391 | /* ensure requested subsystems are available for use */ |
Ben Skeggs | 10caad3 | 2013-04-25 11:43:54 +1000 | [diff] [blame] | 392 | for (i = 1, c = 1; i < NVDEV_SUBDEV_NR; i++) { |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 393 | if (!(oclass = device->oclass[i]) || (disable & (1ULL << i))) |
| 394 | continue; |
| 395 | |
Ben Skeggs | 10caad3 | 2013-04-25 11:43:54 +1000 | [diff] [blame] | 396 | if (device->subdev[i]) { |
Ben Skeggs | 7234d02 | 2012-10-02 10:30:34 +1000 | [diff] [blame] | 397 | nouveau_object_ref(device->subdev[i], |
| 398 | &devobj->subdev[i]); |
Ben Skeggs | 10caad3 | 2013-04-25 11:43:54 +1000 | [diff] [blame] | 399 | continue; |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 400 | } |
| 401 | |
Ben Skeggs | 10caad3 | 2013-04-25 11:43:54 +1000 | [diff] [blame] | 402 | ret = nouveau_object_ctor(nv_object(device), NULL, |
| 403 | oclass, NULL, i, |
| 404 | &devobj->subdev[i]); |
| 405 | if (ret == -ENODEV) |
| 406 | continue; |
| 407 | if (ret) |
| 408 | return ret; |
| 409 | |
Ben Skeggs | 61b365a | 2013-11-27 09:46:56 +1000 | [diff] [blame] | 410 | device->subdev[i] = devobj->subdev[i]; |
| 411 | |
Ben Skeggs | 7234d02 | 2012-10-02 10:30:34 +1000 | [diff] [blame] | 412 | /* note: can't init *any* subdevs until devinit has been run |
| 413 | * due to not knowing exactly what the vbios init tables will |
| 414 | * mess with. devinit also can't be run until all of its |
| 415 | * dependencies have been created. |
| 416 | * |
| 417 | * this code delays init of any subdev until all of devinit's |
| 418 | * dependencies have been created, and then initialises each |
| 419 | * subdev in turn as they're created. |
| 420 | */ |
| 421 | while (i >= NVDEV_SUBDEV_DEVINIT_LAST && c <= i) { |
| 422 | struct nouveau_object *subdev = devobj->subdev[c++]; |
| 423 | if (subdev && !nv_iclass(subdev, NV_ENGINE_CLASS)) { |
| 424 | ret = nouveau_object_inc(subdev); |
| 425 | if (ret) |
| 426 | return ret; |
Ben Skeggs | 10caad3 | 2013-04-25 11:43:54 +1000 | [diff] [blame] | 427 | atomic_dec(&nv_object(device)->usecount); |
| 428 | } else |
| 429 | if (subdev) { |
| 430 | nouveau_subdev_reset(subdev); |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 431 | } |
| 432 | } |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | static void |
| 439 | nouveau_devobj_dtor(struct nouveau_object *object) |
| 440 | { |
| 441 | struct nouveau_devobj *devobj = (void *)object; |
| 442 | int i; |
| 443 | |
| 444 | for (i = NVDEV_SUBDEV_NR - 1; i >= 0; i--) |
| 445 | nouveau_object_ref(NULL, &devobj->subdev[i]); |
| 446 | |
| 447 | nouveau_parent_destroy(&devobj->base); |
| 448 | } |
| 449 | |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 450 | static struct nouveau_ofuncs |
| 451 | nouveau_devobj_ofuncs = { |
| 452 | .ctor = nouveau_devobj_ctor, |
| 453 | .dtor = nouveau_devobj_dtor, |
Ben Skeggs | 10caad3 | 2013-04-25 11:43:54 +1000 | [diff] [blame] | 454 | .init = _nouveau_parent_init, |
| 455 | .fini = _nouveau_parent_fini, |
Ben Skeggs | d01c309 | 2014-08-10 04:10:21 +1000 | [diff] [blame] | 456 | .mthd = nouveau_devobj_mthd, |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 457 | .rd08 = nouveau_devobj_rd08, |
| 458 | .rd16 = nouveau_devobj_rd16, |
| 459 | .rd32 = nouveau_devobj_rd32, |
| 460 | .wr08 = nouveau_devobj_wr08, |
| 461 | .wr16 = nouveau_devobj_wr16, |
| 462 | .wr32 = nouveau_devobj_wr32, |
| 463 | }; |
| 464 | |
| 465 | /****************************************************************************** |
| 466 | * nouveau_device: engine functions |
| 467 | *****************************************************************************/ |
Ben Skeggs | 79ca277 | 2014-08-10 04:10:20 +1000 | [diff] [blame] | 468 | |
Ben Skeggs | 9aecbad | 2013-04-25 17:56:03 +1000 | [diff] [blame] | 469 | static struct nouveau_oclass |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 470 | nouveau_device_sclass[] = { |
| 471 | { 0x0080, &nouveau_devobj_ofuncs }, |
| 472 | {} |
| 473 | }; |
| 474 | |
Ben Skeggs | 066a5d0 | 2013-04-25 11:35:18 +1000 | [diff] [blame] | 475 | static int |
Ben Skeggs | 79ca277 | 2014-08-10 04:10:20 +1000 | [diff] [blame] | 476 | nouveau_device_event_ctor(void *data, u32 size, struct nvkm_notify *notify) |
| 477 | { |
| 478 | if (!WARN_ON(size != 0)) { |
| 479 | notify->size = 0; |
| 480 | notify->types = 1; |
| 481 | notify->index = 0; |
| 482 | return 0; |
| 483 | } |
| 484 | return -EINVAL; |
| 485 | } |
| 486 | |
| 487 | static const struct nvkm_event_func |
| 488 | nouveau_device_event_func = { |
| 489 | .ctor = nouveau_device_event_ctor, |
| 490 | }; |
| 491 | |
| 492 | static int |
Ben Skeggs | 066a5d0 | 2013-04-25 11:35:18 +1000 | [diff] [blame] | 493 | nouveau_device_fini(struct nouveau_object *object, bool suspend) |
| 494 | { |
| 495 | struct nouveau_device *device = (void *)object; |
Ben Skeggs | 10caad3 | 2013-04-25 11:43:54 +1000 | [diff] [blame] | 496 | struct nouveau_object *subdev; |
| 497 | int ret, i; |
| 498 | |
| 499 | for (i = NVDEV_SUBDEV_NR - 1; i >= 0; i--) { |
| 500 | if ((subdev = device->subdev[i])) { |
| 501 | if (!nv_iclass(subdev, NV_ENGINE_CLASS)) { |
| 502 | ret = nouveau_object_dec(subdev, suspend); |
| 503 | if (ret && suspend) |
| 504 | goto fail; |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | |
Ben Skeggs | ed76a87 | 2014-06-13 12:42:21 +1000 | [diff] [blame] | 509 | ret = nvkm_acpi_fini(device, suspend); |
Ben Skeggs | 10caad3 | 2013-04-25 11:43:54 +1000 | [diff] [blame] | 510 | fail: |
| 511 | for (; ret && i < NVDEV_SUBDEV_NR; i++) { |
| 512 | if ((subdev = device->subdev[i])) { |
| 513 | if (!nv_iclass(subdev, NV_ENGINE_CLASS)) { |
| 514 | ret = nouveau_object_inc(subdev); |
| 515 | if (ret) { |
| 516 | /* XXX */ |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | return ret; |
Ben Skeggs | 066a5d0 | 2013-04-25 11:35:18 +1000 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | static int |
| 526 | nouveau_device_init(struct nouveau_object *object) |
| 527 | { |
| 528 | struct nouveau_device *device = (void *)object; |
Ben Skeggs | 10caad3 | 2013-04-25 11:43:54 +1000 | [diff] [blame] | 529 | struct nouveau_object *subdev; |
Ben Skeggs | ed76a87 | 2014-06-13 12:42:21 +1000 | [diff] [blame] | 530 | int ret, i = 0; |
| 531 | |
| 532 | ret = nvkm_acpi_init(device); |
| 533 | if (ret) |
| 534 | goto fail; |
Ben Skeggs | 10caad3 | 2013-04-25 11:43:54 +1000 | [diff] [blame] | 535 | |
| 536 | for (i = 0; i < NVDEV_SUBDEV_NR; i++) { |
| 537 | if ((subdev = device->subdev[i])) { |
| 538 | if (!nv_iclass(subdev, NV_ENGINE_CLASS)) { |
| 539 | ret = nouveau_object_inc(subdev); |
| 540 | if (ret) |
| 541 | goto fail; |
| 542 | } else { |
| 543 | nouveau_subdev_reset(subdev); |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | ret = 0; |
| 549 | fail: |
| 550 | for (--i; ret && i >= 0; i--) { |
| 551 | if ((subdev = device->subdev[i])) { |
| 552 | if (!nv_iclass(subdev, NV_ENGINE_CLASS)) |
| 553 | nouveau_object_dec(subdev, false); |
| 554 | } |
| 555 | } |
| 556 | |
Ben Skeggs | ed76a87 | 2014-06-13 12:42:21 +1000 | [diff] [blame] | 557 | if (ret) |
| 558 | nvkm_acpi_fini(device, false); |
Ben Skeggs | 10caad3 | 2013-04-25 11:43:54 +1000 | [diff] [blame] | 559 | return ret; |
Ben Skeggs | 066a5d0 | 2013-04-25 11:35:18 +1000 | [diff] [blame] | 560 | } |
| 561 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 562 | static void |
| 563 | nouveau_device_dtor(struct nouveau_object *object) |
| 564 | { |
| 565 | struct nouveau_device *device = (void *)object; |
| 566 | |
Ben Skeggs | 79ca277 | 2014-08-10 04:10:20 +1000 | [diff] [blame] | 567 | nvkm_event_fini(&device->event); |
Ben Skeggs | ed76a87 | 2014-06-13 12:42:21 +1000 | [diff] [blame] | 568 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 569 | mutex_lock(&nv_devices_mutex); |
| 570 | list_del(&device->head); |
| 571 | mutex_unlock(&nv_devices_mutex); |
| 572 | |
Ben Skeggs | dded35d | 2013-04-25 17:23:43 +1000 | [diff] [blame] | 573 | if (nv_subdev(device)->mmio) |
| 574 | iounmap(nv_subdev(device)->mmio); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 575 | |
Ben Skeggs | dded35d | 2013-04-25 17:23:43 +1000 | [diff] [blame] | 576 | nouveau_engine_destroy(&device->base); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 577 | } |
| 578 | |
Alexandre Courbot | 420b946 | 2014-02-17 15:17:26 +0900 | [diff] [blame] | 579 | resource_size_t |
| 580 | nv_device_resource_start(struct nouveau_device *device, unsigned int bar) |
| 581 | { |
| 582 | if (nv_device_is_pci(device)) { |
| 583 | return pci_resource_start(device->pdev, bar); |
| 584 | } else { |
| 585 | struct resource *res; |
| 586 | res = platform_get_resource(device->platformdev, |
| 587 | IORESOURCE_MEM, bar); |
| 588 | if (!res) |
| 589 | return 0; |
| 590 | return res->start; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | resource_size_t |
| 595 | nv_device_resource_len(struct nouveau_device *device, unsigned int bar) |
| 596 | { |
| 597 | if (nv_device_is_pci(device)) { |
| 598 | return pci_resource_len(device->pdev, bar); |
| 599 | } else { |
| 600 | struct resource *res; |
| 601 | res = platform_get_resource(device->platformdev, |
| 602 | IORESOURCE_MEM, bar); |
| 603 | if (!res) |
| 604 | return 0; |
| 605 | return resource_size(res); |
| 606 | } |
| 607 | } |
| 608 | |
Alexandre Courbot | 420b946 | 2014-02-17 15:17:26 +0900 | [diff] [blame] | 609 | int |
| 610 | nv_device_get_irq(struct nouveau_device *device, bool stall) |
| 611 | { |
| 612 | if (nv_device_is_pci(device)) { |
| 613 | return device->pdev->irq; |
| 614 | } else { |
| 615 | return platform_get_irq_byname(device->platformdev, |
| 616 | stall ? "stall" : "nonstall"); |
| 617 | } |
| 618 | } |
| 619 | |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 620 | static struct nouveau_oclass |
| 621 | nouveau_device_oclass = { |
Ben Skeggs | dded35d | 2013-04-25 17:23:43 +1000 | [diff] [blame] | 622 | .handle = NV_ENGINE(DEVICE, 0x00), |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 623 | .ofuncs = &(struct nouveau_ofuncs) { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 624 | .dtor = nouveau_device_dtor, |
Ben Skeggs | 066a5d0 | 2013-04-25 11:35:18 +1000 | [diff] [blame] | 625 | .init = nouveau_device_init, |
| 626 | .fini = nouveau_device_fini, |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 627 | }, |
| 628 | }; |
| 629 | |
| 630 | int |
Alexandre Courbot | 420b946 | 2014-02-17 15:17:26 +0900 | [diff] [blame] | 631 | nouveau_device_create_(void *dev, enum nv_bus_type type, u64 name, |
| 632 | const char *sname, const char *cfg, const char *dbg, |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 633 | int length, void **pobject) |
| 634 | { |
| 635 | struct nouveau_device *device; |
| 636 | int ret = -EEXIST; |
| 637 | |
| 638 | mutex_lock(&nv_devices_mutex); |
| 639 | list_for_each_entry(device, &nv_devices, head) { |
| 640 | if (device->handle == name) |
| 641 | goto done; |
| 642 | } |
| 643 | |
Ben Skeggs | dded35d | 2013-04-25 17:23:43 +1000 | [diff] [blame] | 644 | ret = nouveau_engine_create_(NULL, NULL, &nouveau_device_oclass, true, |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 645 | "DEVICE", "device", length, pobject); |
| 646 | device = *pobject; |
| 647 | if (ret) |
| 648 | goto done; |
| 649 | |
Alexandre Courbot | 420b946 | 2014-02-17 15:17:26 +0900 | [diff] [blame] | 650 | switch (type) { |
| 651 | case NOUVEAU_BUS_PCI: |
| 652 | device->pdev = dev; |
| 653 | break; |
| 654 | case NOUVEAU_BUS_PLATFORM: |
| 655 | device->platformdev = dev; |
| 656 | break; |
| 657 | } |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 658 | device->handle = name; |
| 659 | device->cfgopt = cfg; |
| 660 | device->dbgopt = dbg; |
| 661 | device->name = sname; |
| 662 | |
| 663 | nv_subdev(device)->debug = nouveau_dbgopt(device->dbgopt, "DEVICE"); |
Ben Skeggs | 9aecbad | 2013-04-25 17:56:03 +1000 | [diff] [blame] | 664 | nv_engine(device)->sclass = nouveau_device_sclass; |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 665 | list_add(&device->head, &nv_devices); |
Ben Skeggs | ed76a87 | 2014-06-13 12:42:21 +1000 | [diff] [blame] | 666 | |
Ben Skeggs | 79ca277 | 2014-08-10 04:10:20 +1000 | [diff] [blame] | 667 | ret = nvkm_event_init(&nouveau_device_event_func, 1, 1, |
| 668 | &device->event); |
Ben Skeggs | 9274f4a | 2012-07-06 07:36:43 +1000 | [diff] [blame] | 669 | done: |
| 670 | mutex_unlock(&nv_devices_mutex); |
| 671 | return ret; |
| 672 | } |