nouveau_pci.c revision 1.37
1/* $NetBSD: nouveau_pci.c,v 1.37 2023/03/01 08:42:34 riastradh 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.37 2023/03/01 08:42:34 riastradh 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_pci.h> 59 60#include <core/device.h> 61#include <core/pci.h> 62 63#include "nouveau_drv.h" 64#include "nouveau_pci.h" 65 66struct drm_device; 67 68MODULE(MODULE_CLASS_DRIVER, nouveau_pci, "nouveau,drmkms_pci"); 69 70SIMPLEQ_HEAD(nouveau_pci_task_head, nouveau_pci_task); 71 72struct nouveau_pci_softc { 73 device_t sc_dev; 74 struct pci_attach_args sc_pa; 75 struct lwp *sc_task_thread; 76 struct nouveau_pci_task_head sc_tasks; 77 struct workqueue *sc_task_wq; 78 struct drm_device *sc_drm_dev; 79 struct pci_dev sc_pci_dev; 80 struct nvkm_device *sc_nv_dev; 81 bool sc_pci_attached; 82 bool sc_nvdev_inited; 83 bool sc_dev_registered; 84}; 85 86static int nouveau_pci_match(device_t, cfdata_t, void *); 87static void nouveau_pci_attach(device_t, device_t, void *); 88static void nouveau_pci_attach_real(device_t); 89static int nouveau_pci_detach(device_t, int); 90 91static bool nouveau_pci_suspend(device_t, const pmf_qual_t *); 92static bool nouveau_pci_resume(device_t, const pmf_qual_t *); 93 94static void nouveau_pci_task_work(struct work *, void *); 95 96CFATTACH_DECL_NEW(nouveau_pci, sizeof(struct nouveau_pci_softc), 97 nouveau_pci_match, nouveau_pci_attach, nouveau_pci_detach, NULL); 98 99/* Kludge to get this from nouveau_drm.c. */ 100extern struct drm_driver *const nouveau_drm_driver_pci; 101 102static int 103nouveau_pci_match(device_t parent, cfdata_t match, void *aux) 104{ 105 const struct pci_attach_args *const pa = aux; 106 struct pci_dev pdev; 107 struct nvkm_device *device; 108 int ret; 109 110 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA && 111 PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA_SGS) 112 return 0; 113 114 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY) 115 return 0; 116 117 /* 118 * NetBSD drm2/5.6 doesn't support Ampere (GTX 30 series) based cards: 119 * 0x2080-0x20ff GA100 120 * 0x2200-0x227f GA102 121 * 0x2300-0x237f GA103 122 * 0x2480-0x24ff GA104 123 * 0x2500-0x257f GA106 124 * 0x2580-0x25ff GA107 125 * 126 * TU116 (GTX 16xx) occupies the space from 0x2180-0x21ff. 127 */ 128 if (PCI_PRODUCT(pa->pa_id) >= 0x1fff && PCI_PRODUCT(pa->pa_id) < 0x2180) 129 return 0; 130 if (PCI_PRODUCT(pa->pa_id) >= 0x21ff) 131 return 0; 132 133 linux_pci_dev_init(&pdev, parent /* XXX bogus */, parent, pa, 0); 134 ret = nvkm_device_pci_new(&pdev, NULL, "error", 135 /* detect */ true, /* mmio */ false, /* subdev_mask */ 0, &device); 136 if (ret == 0) /* don't want to hang onto it */ 137 nvkm_device_del(&device); 138 linux_pci_dev_destroy(&pdev); 139 if (ret) /* failure */ 140 return 0; 141 142 return 6; /* XXX Beat genfb_pci... */ 143} 144 145extern char *nouveau_config; 146extern char *nouveau_debug; 147 148static void 149nouveau_pci_attach(device_t parent, device_t self, void *aux) 150{ 151 struct nouveau_pci_softc *const sc = device_private(self); 152 const struct pci_attach_args *const pa = aux; 153 int error; 154 155 pci_aprint_devinfo(pa, NULL); 156 157 /* Initialize the Linux PCI device descriptor. */ 158 linux_pci_dev_init(&sc->sc_pci_dev, self, device_parent(self), pa, 0); 159 160 sc->sc_dev = self; 161 sc->sc_pa = *pa; 162 sc->sc_task_thread = NULL; 163 SIMPLEQ_INIT(&sc->sc_tasks); 164 error = workqueue_create(&sc->sc_task_wq, "nouveau_pci", 165 &nouveau_pci_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE); 166 if (error) { 167 aprint_error_dev(self, "unable to create workqueue: %d\n", 168 error); 169 sc->sc_task_wq = NULL; 170 return; 171 } 172 173#ifdef FDT 174 /* 175 * XXX Remove the simple framebuffer, assuming that this device 176 * will take over. 177 */ 178 const char *fb_compatible[] = { "simple-framebuffer", NULL }; 179 fdt_remove_bycompat(fb_compatible); 180#endif 181 182 /* 183 * Defer the remainder of initialization until we have mounted 184 * the root file system and can load firmware images. 185 */ 186 config_mountroot(self, &nouveau_pci_attach_real); 187} 188 189static void 190nouveau_pci_attach_real(device_t self) 191{ 192 struct nouveau_pci_softc *const sc = device_private(self); 193 int error; 194 195 /* 196 * Cause any tasks issued synchronously during attach to be 197 * processed at the end of this function. 198 */ 199 sc->sc_task_thread = curlwp; 200 201 /* XXX errno Linux->NetBSD */ 202 error = -nvkm_device_pci_new(&sc->sc_pci_dev, 203 nouveau_config, nouveau_debug, 204 /* detect */ true, /* mmio */ true, /* subdev_mask */ ~0ULL, 205 &sc->sc_nv_dev); 206 if (error) { 207 aprint_error_dev(self, "unable to create nouveau device: %d\n", 208 error); 209 sc->sc_nv_dev = NULL; 210 goto out; 211 } 212 213 sc->sc_drm_dev = drm_dev_alloc(nouveau_drm_driver_pci, self); 214 if (IS_ERR(sc->sc_drm_dev)) { 215 aprint_error_dev(self, "unable to create drm device: %ld\n", 216 PTR_ERR(sc->sc_drm_dev)); 217 sc->sc_drm_dev = NULL; 218 goto out; 219 } 220 221 /* XXX errno Linux->NetBSD */ 222 error = -drm_pci_attach(sc->sc_drm_dev, &sc->sc_pci_dev); 223 if (error) { 224 aprint_error_dev(self, "unable to attach drm: %d\n", error); 225 goto out; 226 } 227 sc->sc_pci_attached = true; 228 229#if NGENFB > 0 230 /* 231 * If MD initialization has selected this as the console device 232 * with a firmware-provided framebuffer address, we may have to 233 * turn it off early, before we are ready to switch the console 234 * over -- something goes wrong if we're still writing to the 235 * firmware-provided framebuffer during nouveau initialization. 236 */ 237 { 238 bool is_console; 239 if (prop_dictionary_get_bool(device_properties(self), "is_console", 240 &is_console) && 241 is_console && 242 genfb_is_console()) 243 wsdisplay_predetach(); 244 } 245#endif 246 247 /* XXX errno Linux->NetBSD */ 248 error = -nouveau_drm_device_init(sc->sc_drm_dev); 249 if (error) { 250 aprint_error_dev(self, "unable to init nouveau: %d\n", error); 251 goto out; 252 } 253 sc->sc_nvdev_inited = true; 254 255 /* XXX errno Linux->NetBSD */ 256 error = -drm_dev_register(sc->sc_drm_dev, 0); 257 if (error) { 258 aprint_error_dev(self, "unable to register drm: %d\n", error); 259 goto out; 260 } 261 sc->sc_dev_registered = true; 262 263 if (!pmf_device_register(self, &nouveau_pci_suspend, 264 &nouveau_pci_resume)) 265 aprint_error_dev(self, "unable to establish power handler\n"); 266 267 while (!SIMPLEQ_EMPTY(&sc->sc_tasks)) { 268 struct nouveau_pci_task *const task = 269 SIMPLEQ_FIRST(&sc->sc_tasks); 270 271 SIMPLEQ_REMOVE_HEAD(&sc->sc_tasks, nt_u.queue); 272 (*task->nt_fn)(task); 273 } 274 275out: /* Cause any subesquent tasks to be processed by the workqueue. */ 276 atomic_store_relaxed(&sc->sc_task_thread, NULL); 277} 278 279static int 280nouveau_pci_detach(device_t self, int flags) 281{ 282 struct nouveau_pci_softc *const sc = device_private(self); 283 int error; 284 285 /* XXX Check for in-use before tearing it all down... */ 286 error = config_detach_children(self, flags); 287 if (error) 288 return error; 289 290 pmf_device_deregister(self); 291 if (sc->sc_dev_registered) 292 drm_dev_unregister(sc->sc_drm_dev); 293 if (sc->sc_nvdev_inited) 294 nouveau_drm_device_fini(sc->sc_drm_dev); 295 if (sc->sc_pci_attached) 296 drm_pci_detach(sc->sc_drm_dev); 297 if (sc->sc_drm_dev) { 298 drm_dev_put(sc->sc_drm_dev); 299 sc->sc_drm_dev = NULL; 300 } 301 if (sc->sc_nv_dev) 302 nvkm_device_del(&sc->sc_nv_dev); 303 if (sc->sc_task_wq) { 304 workqueue_destroy(sc->sc_task_wq); 305 sc->sc_task_wq = NULL; 306 } 307 linux_pci_dev_destroy(&sc->sc_pci_dev); 308 309 return 0; 310} 311 312/* 313 * XXX Synchronize with nouveau_do_suspend in nouveau_drm.c. 314 */ 315static bool 316nouveau_pci_suspend(device_t self, const pmf_qual_t *qual __unused) 317{ 318 struct nouveau_pci_softc *const sc = device_private(self); 319 320 return nouveau_pmops_suspend(sc->sc_drm_dev) == 0; 321} 322 323static bool 324nouveau_pci_resume(device_t self, const pmf_qual_t *qual) 325{ 326 struct nouveau_pci_softc *const sc = device_private(self); 327 328 return nouveau_pmops_resume(sc->sc_drm_dev) == 0; 329} 330 331static void 332nouveau_pci_task_work(struct work *work, void *cookie __unused) 333{ 334 struct nouveau_pci_task *const task = container_of(work, 335 struct nouveau_pci_task, nt_u.work); 336 337 (*task->nt_fn)(task); 338} 339 340void 341nouveau_pci_task_schedule(device_t self, struct nouveau_pci_task *task) 342{ 343 struct nouveau_pci_softc *const sc = device_private(self); 344 345 if (atomic_load_relaxed(&sc->sc_task_thread) == curlwp) 346 SIMPLEQ_INSERT_TAIL(&sc->sc_tasks, task, nt_u.queue); 347 else 348 workqueue_enqueue(sc->sc_task_wq, &task->nt_u.work, NULL); 349} 350 351extern struct drm_driver *const nouveau_drm_driver_stub; /* XXX */ 352extern struct drm_driver *const nouveau_drm_driver_pci; /* XXX */ 353 354static int 355nouveau_pci_modcmd(modcmd_t cmd, void *arg __unused) 356{ 357 358 switch (cmd) { 359 case MODULE_CMD_INIT: 360 *nouveau_drm_driver_pci = *nouveau_drm_driver_stub; 361 nouveau_drm_driver_pci->request_irq = drm_pci_request_irq; 362 nouveau_drm_driver_pci->free_irq = drm_pci_free_irq; 363#if 0 /* XXX nouveau acpi */ 364 nouveau_register_dsm_handler(); 365#endif 366 break; 367 case MODULE_CMD_FINI: 368#if 0 /* XXX nouveau acpi */ 369 nouveau_unregister_dsm_handler(); 370#endif 371 break; 372 default: 373 return ENOTTY; 374 } 375 376 return 0; 377} 378