nouveau_pci.c revision 1.31
1/* $NetBSD: nouveau_pci.c,v 1.31 2021/12/19 11:05:20 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.31 2021/12/19 11:05:20 riastradh Exp $"); 34 35#ifdef _KERNEL_OPT 36#if defined(__arm__) || defined(__aarch64__) 37#include "opt_fdt.h" 38#endif 39#endif 40 41#include <sys/types.h> 42#include <sys/device.h> 43#include <sys/queue.h> 44#include <sys/workqueue.h> 45#include <sys/module.h> 46 47#ifdef FDT 48#include <dev/fdt/fdtvar.h> 49#endif 50 51#include <drm/drm_pci.h> 52 53#include <core/device.h> 54#include <core/pci.h> 55 56#include "nouveau_drv.h" 57#include "nouveau_pci.h" 58 59struct drm_device; 60 61MODULE(MODULE_CLASS_DRIVER, nouveau_pci, "nouveau,drmkms_pci"); 62 63SIMPLEQ_HEAD(nouveau_pci_task_head, nouveau_pci_task); 64 65struct nouveau_pci_softc { 66 device_t sc_dev; 67 struct pci_attach_args sc_pa; 68 enum { 69 NOUVEAU_TASK_ATTACH, 70 NOUVEAU_TASK_WORKQUEUE, 71 } sc_task_state; 72 union { 73 struct workqueue *workqueue; 74 struct nouveau_pci_task_head attach; 75 } sc_task_u; 76 struct drm_device *sc_drm_dev; 77 struct pci_dev sc_pci_dev; 78 struct nvkm_device *sc_nv_dev; 79 bool sc_pci_attached; 80 bool sc_dev_registered; 81}; 82 83static int nouveau_pci_match(device_t, cfdata_t, void *); 84static void nouveau_pci_attach(device_t, device_t, void *); 85static void nouveau_pci_attach_real(device_t); 86static int nouveau_pci_detach(device_t, int); 87 88static bool nouveau_pci_suspend(device_t, const pmf_qual_t *); 89static bool nouveau_pci_resume(device_t, const pmf_qual_t *); 90 91static void nouveau_pci_task_work(struct work *, void *); 92 93CFATTACH_DECL_NEW(nouveau_pci, sizeof(struct nouveau_pci_softc), 94 nouveau_pci_match, nouveau_pci_attach, nouveau_pci_detach, NULL); 95 96/* Kludge to get this from nouveau_drm.c. */ 97extern struct drm_driver *const nouveau_drm_driver_pci; 98 99static int 100nouveau_pci_match(device_t parent, cfdata_t match, void *aux) 101{ 102 const struct pci_attach_args *const pa = aux; 103 struct pci_dev pdev; 104 struct nvkm_device *device; 105 int ret; 106 107 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA && 108 PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA_SGS) 109 return 0; 110 111 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY) 112 return 0; 113 114 /* 115 * NetBSD drm2 doesn't support Pascal, Volta or Turing based cards: 116 * 0x1580-0x15ff GP100 117 * 0x1b00-0x1b7f GP102 118 * 0x1b80-0x1bff GP104 119 * 0x1c00-0x1c7f GP106 120 * 0x1c80-0x1cff GP107 121 * 0x1d00-0x1d7f GP108 122 * 0x1d80-0x1dff GV100 123 * 0x1e00-0x1e7f TU102 124 * 0x1e80-0x1eff TU104 125 * 0x1f00-0x1f7f TU106 126 * 0x1f80-0x1fff TU117 127 * 0x2180-0x21ff TU116 128 * 129 * reduce this to >= 1580, so that new chipsets not explictly 130 * listed above will be picked up. 131 * 132 * XXX perhaps switch this to explicitly match known list. 133 */ 134 if (PCI_PRODUCT(pa->pa_id) >= 0x1580) 135 return 0; 136 137 linux_pci_dev_init(&pdev, parent /* XXX bogus */, parent, pa, 0); 138 ret = nvkm_device_pci_new(&pdev, NULL, "error", 139 /* detect */ true, /* mmio */ false, /* subdev_mask */ 0, &device); 140 if (ret == 0) /* don't want to hang onto it */ 141 nvkm_device_del(&device); 142 linux_pci_dev_destroy(&pdev); 143 if (ret) /* failure */ 144 return 0; 145 146 return 6; /* XXX Beat genfb_pci... */ 147} 148 149extern char *nouveau_config; 150extern char *nouveau_debug; 151 152static void 153nouveau_pci_attach(device_t parent, device_t self, void *aux) 154{ 155 struct nouveau_pci_softc *const sc = device_private(self); 156 const struct pci_attach_args *const pa = aux; 157 158 pci_aprint_devinfo(pa, NULL); 159 160 if (!pmf_device_register(self, &nouveau_pci_suspend, &nouveau_pci_resume)) 161 aprint_error_dev(self, "unable to establish power handler\n"); 162 163 /* 164 * Trivial initialization first; the rest will come after we 165 * have mounted the root file system and can load firmware 166 * images. 167 */ 168 sc->sc_dev = NULL; 169 sc->sc_pa = *pa; 170 171#ifdef FDT 172 /* 173 * XXX Remove the simple framebuffer, assuming that this device 174 * will take over. 175 */ 176 const char *fb_compatible[] = { "simple-framebuffer", NULL }; 177 fdt_remove_bycompat(fb_compatible); 178#endif 179 180 config_mountroot(self, &nouveau_pci_attach_real); 181} 182 183static void 184nouveau_pci_attach_real(device_t self) 185{ 186 struct nouveau_pci_softc *const sc = device_private(self); 187 const struct pci_attach_args *const pa = &sc->sc_pa; 188 int error; 189 190 sc->sc_dev = self; 191 sc->sc_task_state = NOUVEAU_TASK_ATTACH; 192 SIMPLEQ_INIT(&sc->sc_task_u.attach); 193 194 /* Initialize the Linux PCI device descriptor. */ 195 linux_pci_dev_init(&sc->sc_pci_dev, self, device_parent(self), pa, 0); 196 197 /* XXX errno Linux->NetBSD */ 198 error = -nvkm_device_pci_new(&sc->sc_pci_dev, 199 nouveau_config, nouveau_debug, 200 /* detect */ true, /* mmio */ true, /* subdev_mask */ ~0ULL, 201 &sc->sc_nv_dev); 202 if (error) { 203 aprint_error_dev(self, "unable to create nouveau device: %d\n", 204 error); 205 sc->sc_nv_dev = NULL; 206 return; 207 } 208 209 sc->sc_drm_dev = drm_dev_alloc(nouveau_drm_driver_pci, self); 210 if (IS_ERR(sc->sc_drm_dev)) { 211 aprint_error_dev(self, "unable to create drm device: %ld\n", 212 PTR_ERR(sc->sc_drm_dev)); 213 sc->sc_drm_dev = NULL; 214 return; 215 } 216 217 /* XXX errno Linux->NetBSD */ 218 error = -drm_pci_attach(sc->sc_drm_dev, pa, &sc->sc_pci_dev); 219 if (error) { 220 aprint_error_dev(self, "unable to attach drm: %d\n", error); 221 return; 222 } 223 sc->sc_pci_attached = true; 224 225 /* XXX errno Linux->NetBSD */ 226 error = -nouveau_drm_device_init(sc->sc_drm_dev); 227 if (error) { 228 aprint_error_dev(self, "unable to init nouveau: %d\n", error); 229 return; 230 } 231 232 /* XXX errno Linux->NetBSD */ 233 error = -drm_dev_register(sc->sc_drm_dev, 0); 234 if (error) { 235 aprint_error_dev(self, "unable to register drm: %d\n", error); 236 return; 237 } 238 sc->sc_dev_registered = true; 239 240 while (!SIMPLEQ_EMPTY(&sc->sc_task_u.attach)) { 241 struct nouveau_pci_task *const task = 242 SIMPLEQ_FIRST(&sc->sc_task_u.attach); 243 244 SIMPLEQ_REMOVE_HEAD(&sc->sc_task_u.attach, nt_u.queue); 245 (*task->nt_fn)(task); 246 } 247 248 sc->sc_task_state = NOUVEAU_TASK_WORKQUEUE; 249 error = workqueue_create(&sc->sc_task_u.workqueue, "nouveau_pci", 250 &nouveau_pci_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE); 251 if (error) { 252 aprint_error_dev(self, "unable to create workqueue: %d\n", 253 error); 254 sc->sc_task_u.workqueue = NULL; 255 return; 256 } 257} 258 259static int 260nouveau_pci_detach(device_t self, int flags) 261{ 262 struct nouveau_pci_softc *const sc = device_private(self); 263 int error; 264 265 if (sc->sc_dev == NULL) 266 /* Not done attaching. */ 267 return EBUSY; 268 269 /* XXX Check for in-use before tearing it all down... */ 270 error = config_detach_children(self, flags); 271 if (error) 272 return error; 273 274 if (sc->sc_task_state == NOUVEAU_TASK_ATTACH) 275 goto out0; 276 if (sc->sc_task_u.workqueue != NULL) { 277 workqueue_destroy(sc->sc_task_u.workqueue); 278 sc->sc_task_u.workqueue = NULL; 279 } 280 281 if (sc->sc_nv_dev == NULL) 282 goto out0; 283 if (sc->sc_drm_dev == NULL) 284 goto out1; 285 if (!sc->sc_pci_attached) 286 goto out2; 287 if (!sc->sc_dev_registered) 288 goto out3; 289 290 drm_dev_unregister(sc->sc_drm_dev); 291out3: drm_pci_detach(sc->sc_drm_dev); 292out2: drm_dev_put(sc->sc_drm_dev); 293 sc->sc_drm_dev = NULL; 294out1: nvkm_device_del(&sc->sc_nv_dev); 295out0: linux_pci_dev_destroy(&sc->sc_pci_dev); 296 pmf_device_deregister(self); 297 return 0; 298} 299 300/* 301 * XXX Synchronize with nouveau_do_suspend in nouveau_drm.c. 302 */ 303static bool 304nouveau_pci_suspend(device_t self, const pmf_qual_t *qual __unused) 305{ 306 struct nouveau_pci_softc *const sc = device_private(self); 307 308 return nouveau_pmops_suspend(sc->sc_drm_dev) == 0; 309} 310 311static bool 312nouveau_pci_resume(device_t self, const pmf_qual_t *qual) 313{ 314 struct nouveau_pci_softc *const sc = device_private(self); 315 316 return nouveau_pmops_resume(sc->sc_drm_dev) == 0; 317} 318 319static void 320nouveau_pci_task_work(struct work *work, void *cookie __unused) 321{ 322 struct nouveau_pci_task *const task = container_of(work, 323 struct nouveau_pci_task, nt_u.work); 324 325 (*task->nt_fn)(task); 326} 327 328int 329nouveau_pci_task_schedule(device_t self, struct nouveau_pci_task *task) 330{ 331 struct nouveau_pci_softc *const sc = device_private(self); 332 333 switch (sc->sc_task_state) { 334 case NOUVEAU_TASK_ATTACH: 335 SIMPLEQ_INSERT_TAIL(&sc->sc_task_u.attach, task, nt_u.queue); 336 return 0; 337 case NOUVEAU_TASK_WORKQUEUE: 338 if (sc->sc_task_u.workqueue == NULL) { 339 aprint_error_dev(self, "unable to schedule task\n"); 340 return EIO; 341 } 342 workqueue_enqueue(sc->sc_task_u.workqueue, &task->nt_u.work, 343 NULL); 344 return 0; 345 default: 346 panic("nouveau in invalid task state: %d\n", 347 (int)sc->sc_task_state); 348 } 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->set_busid = drm_pci_set_busid; 362 nouveau_drm_driver_pci->request_irq = drm_pci_request_irq; 363 nouveau_drm_driver_pci->free_irq = drm_pci_free_irq; 364#if 0 /* XXX nouveau acpi */ 365 nouveau_register_dsm_handler(); 366#endif 367 break; 368 case MODULE_CMD_FINI: 369#if 0 /* XXX nouveau acpi */ 370 nouveau_unregister_dsm_handler(); 371#endif 372 break; 373 default: 374 return ENOTTY; 375 } 376 377 return 0; 378} 379