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