1 1.38 riastrad /* $NetBSD: nouveau_pci.c,v 1.38 2023/08/07 16:34:57 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /*- 4 1.1 riastrad * Copyright (c) 2015 The NetBSD Foundation, Inc. 5 1.1 riastrad * All rights reserved. 6 1.1 riastrad * 7 1.1 riastrad * This code is derived from software contributed to The NetBSD Foundation 8 1.1 riastrad * by Taylor R. Campbell. 9 1.1 riastrad * 10 1.1 riastrad * Redistribution and use in source and binary forms, with or without 11 1.1 riastrad * modification, are permitted provided that the following conditions 12 1.1 riastrad * are met: 13 1.1 riastrad * 1. Redistributions of source code must retain the above copyright 14 1.1 riastrad * notice, this list of conditions and the following disclaimer. 15 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 riastrad * notice, this list of conditions and the following disclaimer in the 17 1.1 riastrad * documentation and/or other materials provided with the distribution. 18 1.1 riastrad * 19 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE. 30 1.1 riastrad */ 31 1.1 riastrad 32 1.1 riastrad #include <sys/cdefs.h> 33 1.38 riastrad __KERNEL_RCSID(0, "$NetBSD: nouveau_pci.c,v 1.38 2023/08/07 16:34:57 riastradh Exp $"); 34 1.26 jmcneill 35 1.26 jmcneill #ifdef _KERNEL_OPT 36 1.37 riastrad #include "genfb.h" 37 1.26 jmcneill #if defined(__arm__) || defined(__aarch64__) 38 1.26 jmcneill #include "opt_fdt.h" 39 1.26 jmcneill #endif 40 1.26 jmcneill #endif 41 1.1 riastrad 42 1.1 riastrad #include <sys/types.h> 43 1.34 riastrad #include <sys/atomic.h> 44 1.1 riastrad #include <sys/device.h> 45 1.1 riastrad #include <sys/queue.h> 46 1.1 riastrad #include <sys/workqueue.h> 47 1.4 jmcneill #include <sys/module.h> 48 1.1 riastrad 49 1.26 jmcneill #ifdef FDT 50 1.26 jmcneill #include <dev/fdt/fdtvar.h> 51 1.26 jmcneill #endif 52 1.26 jmcneill 53 1.37 riastrad #if NGENFB > 0 54 1.37 riastrad #include <dev/wscons/wsdisplayvar.h> 55 1.37 riastrad #include <dev/wsfb/genfbvar.h> 56 1.37 riastrad #endif 57 1.37 riastrad 58 1.38 riastrad #include <drm/drm_ioctl.h> 59 1.29 riastrad #include <drm/drm_pci.h> 60 1.29 riastrad 61 1.29 riastrad #include <core/device.h> 62 1.29 riastrad #include <core/pci.h> 63 1.29 riastrad 64 1.27 riastrad #include "nouveau_drv.h" 65 1.1 riastrad #include "nouveau_pci.h" 66 1.1 riastrad 67 1.28 riastrad struct drm_device; 68 1.28 riastrad 69 1.4 jmcneill MODULE(MODULE_CLASS_DRIVER, nouveau_pci, "nouveau,drmkms_pci"); 70 1.1 riastrad 71 1.4 jmcneill SIMPLEQ_HEAD(nouveau_pci_task_head, nouveau_pci_task); 72 1.4 jmcneill 73 1.4 jmcneill struct nouveau_pci_softc { 74 1.1 riastrad device_t sc_dev; 75 1.24 wiz struct pci_attach_args sc_pa; 76 1.34 riastrad struct lwp *sc_task_thread; 77 1.34 riastrad struct nouveau_pci_task_head sc_tasks; 78 1.34 riastrad struct workqueue *sc_task_wq; 79 1.1 riastrad struct drm_device *sc_drm_dev; 80 1.1 riastrad struct pci_dev sc_pci_dev; 81 1.12 riastrad struct nvkm_device *sc_nv_dev; 82 1.30 riastrad bool sc_pci_attached; 83 1.34 riastrad bool sc_nvdev_inited; 84 1.30 riastrad bool sc_dev_registered; 85 1.1 riastrad }; 86 1.1 riastrad 87 1.4 jmcneill static int nouveau_pci_match(device_t, cfdata_t, void *); 88 1.4 jmcneill static void nouveau_pci_attach(device_t, device_t, void *); 89 1.24 wiz static void nouveau_pci_attach_real(device_t); 90 1.4 jmcneill static int nouveau_pci_detach(device_t, int); 91 1.1 riastrad 92 1.4 jmcneill static bool nouveau_pci_suspend(device_t, const pmf_qual_t *); 93 1.4 jmcneill static bool nouveau_pci_resume(device_t, const pmf_qual_t *); 94 1.1 riastrad 95 1.4 jmcneill static void nouveau_pci_task_work(struct work *, void *); 96 1.1 riastrad 97 1.4 jmcneill CFATTACH_DECL_NEW(nouveau_pci, sizeof(struct nouveau_pci_softc), 98 1.4 jmcneill nouveau_pci_match, nouveau_pci_attach, nouveau_pci_detach, NULL); 99 1.1 riastrad 100 1.1 riastrad /* Kludge to get this from nouveau_drm.c. */ 101 1.14 riastrad extern struct drm_driver *const nouveau_drm_driver_pci; 102 1.1 riastrad 103 1.1 riastrad static int 104 1.4 jmcneill nouveau_pci_match(device_t parent, cfdata_t match, void *aux) 105 1.1 riastrad { 106 1.1 riastrad const struct pci_attach_args *const pa = aux; 107 1.19 riastrad struct pci_dev pdev; 108 1.19 riastrad struct nvkm_device *device; 109 1.19 riastrad int ret; 110 1.1 riastrad 111 1.1 riastrad if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA && 112 1.1 riastrad PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA_SGS) 113 1.1 riastrad return 0; 114 1.1 riastrad 115 1.1 riastrad if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY) 116 1.1 riastrad return 0; 117 1.1 riastrad 118 1.10 mrg /* 119 1.35 riastrad * NetBSD drm2/5.6 doesn't support Ampere (GTX 30 series) based cards: 120 1.35 riastrad * 0x2080-0x20ff GA100 121 1.35 riastrad * 0x2200-0x227f GA102 122 1.35 riastrad * 0x2300-0x237f GA103 123 1.35 riastrad * 0x2480-0x24ff GA104 124 1.35 riastrad * 0x2500-0x257f GA106 125 1.35 riastrad * 0x2580-0x25ff GA107 126 1.25 mrg * 127 1.35 riastrad * TU116 (GTX 16xx) occupies the space from 0x2180-0x21ff. 128 1.10 mrg */ 129 1.35 riastrad if (PCI_PRODUCT(pa->pa_id) >= 0x1fff && PCI_PRODUCT(pa->pa_id) < 0x2180) 130 1.35 riastrad return 0; 131 1.35 riastrad if (PCI_PRODUCT(pa->pa_id) >= 0x21ff) 132 1.10 mrg return 0; 133 1.10 mrg 134 1.20 riastrad linux_pci_dev_init(&pdev, parent /* XXX bogus */, parent, pa, 0); 135 1.20 riastrad ret = nvkm_device_pci_new(&pdev, NULL, "error", 136 1.20 riastrad /* detect */ true, /* mmio */ false, /* subdev_mask */ 0, &device); 137 1.19 riastrad if (ret == 0) /* don't want to hang onto it */ 138 1.19 riastrad nvkm_device_del(&device); 139 1.19 riastrad linux_pci_dev_destroy(&pdev); 140 1.19 riastrad if (ret) /* failure */ 141 1.19 riastrad return 0; 142 1.19 riastrad 143 1.1 riastrad return 6; /* XXX Beat genfb_pci... */ 144 1.1 riastrad } 145 1.1 riastrad 146 1.3 riastrad extern char *nouveau_config; 147 1.3 riastrad extern char *nouveau_debug; 148 1.3 riastrad 149 1.1 riastrad static void 150 1.4 jmcneill nouveau_pci_attach(device_t parent, device_t self, void *aux) 151 1.1 riastrad { 152 1.4 jmcneill struct nouveau_pci_softc *const sc = device_private(self); 153 1.1 riastrad const struct pci_attach_args *const pa = aux; 154 1.34 riastrad int error; 155 1.1 riastrad 156 1.1 riastrad pci_aprint_devinfo(pa, NULL); 157 1.1 riastrad 158 1.34 riastrad /* Initialize the Linux PCI device descriptor. */ 159 1.34 riastrad linux_pci_dev_init(&sc->sc_pci_dev, self, device_parent(self), pa, 0); 160 1.24 wiz 161 1.34 riastrad sc->sc_dev = self; 162 1.24 wiz sc->sc_pa = *pa; 163 1.34 riastrad sc->sc_task_thread = NULL; 164 1.34 riastrad SIMPLEQ_INIT(&sc->sc_tasks); 165 1.34 riastrad error = workqueue_create(&sc->sc_task_wq, "nouveau_pci", 166 1.34 riastrad &nouveau_pci_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE); 167 1.34 riastrad if (error) { 168 1.34 riastrad aprint_error_dev(self, "unable to create workqueue: %d\n", 169 1.34 riastrad error); 170 1.34 riastrad sc->sc_task_wq = NULL; 171 1.34 riastrad return; 172 1.34 riastrad } 173 1.1 riastrad 174 1.26 jmcneill #ifdef FDT 175 1.26 jmcneill /* 176 1.26 jmcneill * XXX Remove the simple framebuffer, assuming that this device 177 1.26 jmcneill * will take over. 178 1.26 jmcneill */ 179 1.26 jmcneill const char *fb_compatible[] = { "simple-framebuffer", NULL }; 180 1.26 jmcneill fdt_remove_bycompat(fb_compatible); 181 1.26 jmcneill #endif 182 1.26 jmcneill 183 1.34 riastrad /* 184 1.34 riastrad * Defer the remainder of initialization until we have mounted 185 1.34 riastrad * the root file system and can load firmware images. 186 1.34 riastrad */ 187 1.24 wiz config_mountroot(self, &nouveau_pci_attach_real); 188 1.24 wiz } 189 1.16 riastrad 190 1.24 wiz static void 191 1.24 wiz nouveau_pci_attach_real(device_t self) 192 1.24 wiz { 193 1.24 wiz struct nouveau_pci_softc *const sc = device_private(self); 194 1.24 wiz int error; 195 1.1 riastrad 196 1.34 riastrad /* 197 1.34 riastrad * Cause any tasks issued synchronously during attach to be 198 1.34 riastrad * processed at the end of this function. 199 1.34 riastrad */ 200 1.34 riastrad sc->sc_task_thread = curlwp; 201 1.24 wiz 202 1.3 riastrad /* XXX errno Linux->NetBSD */ 203 1.12 riastrad error = -nvkm_device_pci_new(&sc->sc_pci_dev, 204 1.20 riastrad nouveau_config, nouveau_debug, 205 1.20 riastrad /* detect */ true, /* mmio */ true, /* subdev_mask */ ~0ULL, 206 1.3 riastrad &sc->sc_nv_dev); 207 1.3 riastrad if (error) { 208 1.3 riastrad aprint_error_dev(self, "unable to create nouveau device: %d\n", 209 1.3 riastrad error); 210 1.30 riastrad sc->sc_nv_dev = NULL; 211 1.34 riastrad goto out; 212 1.30 riastrad } 213 1.30 riastrad 214 1.30 riastrad sc->sc_drm_dev = drm_dev_alloc(nouveau_drm_driver_pci, self); 215 1.30 riastrad if (IS_ERR(sc->sc_drm_dev)) { 216 1.30 riastrad aprint_error_dev(self, "unable to create drm device: %ld\n", 217 1.30 riastrad PTR_ERR(sc->sc_drm_dev)); 218 1.30 riastrad sc->sc_drm_dev = NULL; 219 1.34 riastrad goto out; 220 1.3 riastrad } 221 1.3 riastrad 222 1.1 riastrad /* XXX errno Linux->NetBSD */ 223 1.33 riastrad error = -drm_pci_attach(sc->sc_drm_dev, &sc->sc_pci_dev); 224 1.1 riastrad if (error) { 225 1.1 riastrad aprint_error_dev(self, "unable to attach drm: %d\n", error); 226 1.34 riastrad goto out; 227 1.1 riastrad } 228 1.30 riastrad sc->sc_pci_attached = true; 229 1.30 riastrad 230 1.37 riastrad #if NGENFB > 0 231 1.37 riastrad /* 232 1.37 riastrad * If MD initialization has selected this as the console device 233 1.37 riastrad * with a firmware-provided framebuffer address, we may have to 234 1.37 riastrad * turn it off early, before we are ready to switch the console 235 1.37 riastrad * over -- something goes wrong if we're still writing to the 236 1.37 riastrad * firmware-provided framebuffer during nouveau initialization. 237 1.37 riastrad */ 238 1.37 riastrad { 239 1.37 riastrad bool is_console; 240 1.37 riastrad if (prop_dictionary_get_bool(device_properties(self), "is_console", 241 1.37 riastrad &is_console) && 242 1.37 riastrad is_console && 243 1.37 riastrad genfb_is_console()) 244 1.37 riastrad wsdisplay_predetach(); 245 1.37 riastrad } 246 1.37 riastrad #endif 247 1.37 riastrad 248 1.30 riastrad /* XXX errno Linux->NetBSD */ 249 1.31 riastrad error = -nouveau_drm_device_init(sc->sc_drm_dev); 250 1.31 riastrad if (error) { 251 1.31 riastrad aprint_error_dev(self, "unable to init nouveau: %d\n", error); 252 1.34 riastrad goto out; 253 1.31 riastrad } 254 1.34 riastrad sc->sc_nvdev_inited = true; 255 1.31 riastrad 256 1.31 riastrad /* XXX errno Linux->NetBSD */ 257 1.30 riastrad error = -drm_dev_register(sc->sc_drm_dev, 0); 258 1.30 riastrad if (error) { 259 1.30 riastrad aprint_error_dev(self, "unable to register drm: %d\n", error); 260 1.34 riastrad goto out; 261 1.30 riastrad } 262 1.30 riastrad sc->sc_dev_registered = true; 263 1.1 riastrad 264 1.34 riastrad if (!pmf_device_register(self, &nouveau_pci_suspend, 265 1.34 riastrad &nouveau_pci_resume)) 266 1.34 riastrad aprint_error_dev(self, "unable to establish power handler\n"); 267 1.34 riastrad 268 1.34 riastrad while (!SIMPLEQ_EMPTY(&sc->sc_tasks)) { 269 1.4 jmcneill struct nouveau_pci_task *const task = 270 1.34 riastrad SIMPLEQ_FIRST(&sc->sc_tasks); 271 1.1 riastrad 272 1.34 riastrad SIMPLEQ_REMOVE_HEAD(&sc->sc_tasks, nt_u.queue); 273 1.1 riastrad (*task->nt_fn)(task); 274 1.1 riastrad } 275 1.1 riastrad 276 1.34 riastrad out: /* Cause any subesquent tasks to be processed by the workqueue. */ 277 1.34 riastrad atomic_store_relaxed(&sc->sc_task_thread, NULL); 278 1.1 riastrad } 279 1.1 riastrad 280 1.1 riastrad static int 281 1.4 jmcneill nouveau_pci_detach(device_t self, int flags) 282 1.1 riastrad { 283 1.4 jmcneill struct nouveau_pci_softc *const sc = device_private(self); 284 1.1 riastrad int error; 285 1.1 riastrad 286 1.1 riastrad /* XXX Check for in-use before tearing it all down... */ 287 1.1 riastrad error = config_detach_children(self, flags); 288 1.1 riastrad if (error) 289 1.1 riastrad return error; 290 1.1 riastrad 291 1.34 riastrad pmf_device_deregister(self); 292 1.34 riastrad if (sc->sc_dev_registered) 293 1.34 riastrad drm_dev_unregister(sc->sc_drm_dev); 294 1.34 riastrad if (sc->sc_nvdev_inited) 295 1.34 riastrad nouveau_drm_device_fini(sc->sc_drm_dev); 296 1.34 riastrad if (sc->sc_pci_attached) 297 1.34 riastrad drm_pci_detach(sc->sc_drm_dev); 298 1.34 riastrad if (sc->sc_drm_dev) { 299 1.34 riastrad drm_dev_put(sc->sc_drm_dev); 300 1.34 riastrad sc->sc_drm_dev = NULL; 301 1.1 riastrad } 302 1.34 riastrad if (sc->sc_nv_dev) 303 1.34 riastrad nvkm_device_del(&sc->sc_nv_dev); 304 1.34 riastrad if (sc->sc_task_wq) { 305 1.34 riastrad workqueue_destroy(sc->sc_task_wq); 306 1.34 riastrad sc->sc_task_wq = NULL; 307 1.34 riastrad } 308 1.34 riastrad linux_pci_dev_destroy(&sc->sc_pci_dev); 309 1.1 riastrad 310 1.1 riastrad return 0; 311 1.1 riastrad } 312 1.1 riastrad 313 1.1 riastrad /* 314 1.1 riastrad * XXX Synchronize with nouveau_do_suspend in nouveau_drm.c. 315 1.1 riastrad */ 316 1.1 riastrad static bool 317 1.4 jmcneill nouveau_pci_suspend(device_t self, const pmf_qual_t *qual __unused) 318 1.1 riastrad { 319 1.7 riastrad struct nouveau_pci_softc *const sc = device_private(self); 320 1.38 riastrad struct drm_device *const dev = sc->sc_drm_dev; 321 1.38 riastrad int ret; 322 1.38 riastrad 323 1.38 riastrad drm_suspend_ioctl(dev); 324 1.38 riastrad 325 1.38 riastrad ret = nouveau_pmops_suspend(dev); 326 1.38 riastrad if (ret) 327 1.38 riastrad return false; 328 1.1 riastrad 329 1.38 riastrad return true; 330 1.1 riastrad } 331 1.1 riastrad 332 1.1 riastrad static bool 333 1.4 jmcneill nouveau_pci_resume(device_t self, const pmf_qual_t *qual) 334 1.1 riastrad { 335 1.7 riastrad struct nouveau_pci_softc *const sc = device_private(self); 336 1.38 riastrad struct drm_device *const dev = sc->sc_drm_dev; 337 1.38 riastrad int ret; 338 1.38 riastrad 339 1.38 riastrad ret = nouveau_pmops_resume(sc->sc_drm_dev); 340 1.38 riastrad if (ret) 341 1.38 riastrad goto out; 342 1.1 riastrad 343 1.38 riastrad out: drm_resume_ioctl(dev); 344 1.38 riastrad return ret == 0; 345 1.1 riastrad } 346 1.1 riastrad 347 1.1 riastrad static void 348 1.4 jmcneill nouveau_pci_task_work(struct work *work, void *cookie __unused) 349 1.1 riastrad { 350 1.4 jmcneill struct nouveau_pci_task *const task = container_of(work, 351 1.4 jmcneill struct nouveau_pci_task, nt_u.work); 352 1.1 riastrad 353 1.1 riastrad (*task->nt_fn)(task); 354 1.1 riastrad } 355 1.1 riastrad 356 1.36 riastrad void 357 1.4 jmcneill nouveau_pci_task_schedule(device_t self, struct nouveau_pci_task *task) 358 1.1 riastrad { 359 1.4 jmcneill struct nouveau_pci_softc *const sc = device_private(self); 360 1.1 riastrad 361 1.34 riastrad if (atomic_load_relaxed(&sc->sc_task_thread) == curlwp) 362 1.34 riastrad SIMPLEQ_INSERT_TAIL(&sc->sc_tasks, task, nt_u.queue); 363 1.34 riastrad else 364 1.34 riastrad workqueue_enqueue(sc->sc_task_wq, &task->nt_u.work, NULL); 365 1.1 riastrad } 366 1.4 jmcneill 367 1.21 riastrad extern struct drm_driver *const nouveau_drm_driver_stub; /* XXX */ 368 1.21 riastrad extern struct drm_driver *const nouveau_drm_driver_pci; /* XXX */ 369 1.21 riastrad 370 1.4 jmcneill static int 371 1.4 jmcneill nouveau_pci_modcmd(modcmd_t cmd, void *arg __unused) 372 1.4 jmcneill { 373 1.4 jmcneill 374 1.4 jmcneill switch (cmd) { 375 1.4 jmcneill case MODULE_CMD_INIT: 376 1.21 riastrad *nouveau_drm_driver_pci = *nouveau_drm_driver_stub; 377 1.21 riastrad nouveau_drm_driver_pci->request_irq = drm_pci_request_irq; 378 1.21 riastrad nouveau_drm_driver_pci->free_irq = drm_pci_free_irq; 379 1.4 jmcneill #if 0 /* XXX nouveau acpi */ 380 1.4 jmcneill nouveau_register_dsm_handler(); 381 1.4 jmcneill #endif 382 1.4 jmcneill break; 383 1.4 jmcneill case MODULE_CMD_FINI: 384 1.4 jmcneill #if 0 /* XXX nouveau acpi */ 385 1.4 jmcneill nouveau_unregister_dsm_handler(); 386 1.4 jmcneill #endif 387 1.4 jmcneill break; 388 1.4 jmcneill default: 389 1.4 jmcneill return ENOTTY; 390 1.4 jmcneill } 391 1.4 jmcneill 392 1.4 jmcneill return 0; 393 1.4 jmcneill } 394