nouveau_pci.c revision 1.13
1/* $NetBSD: nouveau_pci.c,v 1.13 2018/08/27 07:35:41 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.13 2018/08/27 07:35:41 riastradh Exp $"); 34 35#include <sys/types.h> 36#include <sys/device.h> 37#include <sys/queue.h> 38#include <sys/workqueue.h> 39#include <sys/module.h> 40 41#include <drm/drmP.h> 42 43#include <core/device.h> 44#include <core/pci.h> 45 46#include "nouveau_drm.h" 47#include "nouveau_pci.h" 48 49MODULE(MODULE_CLASS_DRIVER, nouveau_pci, "nouveau,drmkms_pci"); 50 51SIMPLEQ_HEAD(nouveau_pci_task_head, nouveau_pci_task); 52 53struct nouveau_pci_softc { 54 device_t sc_dev; 55 enum { 56 NOUVEAU_TASK_ATTACH, 57 NOUVEAU_TASK_WORKQUEUE, 58 } sc_task_state; 59 union { 60 struct workqueue *workqueue; 61 struct nouveau_pci_task_head attach; 62 } sc_task_u; 63 struct drm_device *sc_drm_dev; 64 struct pci_dev sc_pci_dev; 65 struct nvkm_device *sc_nv_dev; 66}; 67 68static int nouveau_pci_match(device_t, cfdata_t, void *); 69static void nouveau_pci_attach(device_t, device_t, void *); 70static int nouveau_pci_detach(device_t, int); 71 72static bool nouveau_pci_suspend(device_t, const pmf_qual_t *); 73static bool nouveau_pci_resume(device_t, const pmf_qual_t *); 74 75static void nouveau_pci_task_work(struct work *, void *); 76 77CFATTACH_DECL_NEW(nouveau_pci, sizeof(struct nouveau_pci_softc), 78 nouveau_pci_match, nouveau_pci_attach, nouveau_pci_detach, NULL); 79 80/* Kludge to get this from nouveau_drm.c. */ 81extern struct drm_driver *const nouveau_drm_driver; 82 83static int 84nouveau_pci_match(device_t parent, cfdata_t match, void *aux) 85{ 86 const struct pci_attach_args *const pa = aux; 87 88 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA && 89 PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA_SGS) 90 return 0; 91 92 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY) 93 return 0; 94 95#define IS_BETWEEN(x,y) \ 96 (PCI_PRODUCT(pa->pa_id) >= (x) && PCI_PRODUCT(pa->pa_id) <= (y)) 97 /* 98 * NetBSD drm2 needs missing-so-far firmware for Maxwell-based cards: 99 * 0x1380-0x13bf GM107 100 */ 101 if (IS_BETWEEN(0x1380, 0x13bf)) 102 return 0; 103 104 /* 105 * NetBSD drm2 doesn't support Pascal-based cards: 106 * 0x1580-0x15ff GP100 107 * 0x1b00-0x1b7f GP102 108 * 0x1b80-0x1bff GP104 109 * 0x1c00-0x1c7f GP106 110 * 0x1c80-0x1cff GP107 111 * 0x1d00-0x1d7f GP108 112 * 0x1d80-0x1dff GV100 113 */ 114 115 if (IS_BETWEEN(0x1580, 0x15ff) || 116 IS_BETWEEN(0x1b00, 0x1b7f) || 117 IS_BETWEEN(0x1b80, 0x1bff) || 118 IS_BETWEEN(0x1c00, 0x1c7f) || 119 IS_BETWEEN(0x1c80, 0x1cff) || 120 IS_BETWEEN(0x1d00, 0x1d7f) || 121 IS_BETWEEN(0x1d80, 0x1dff)) 122 return 0; 123#undef IS_BETWEEN 124 125 return 6; /* XXX Beat genfb_pci... */ 126} 127 128extern char *nouveau_config; 129extern char *nouveau_debug; 130 131static void 132nouveau_pci_attach(device_t parent, device_t self, void *aux) 133{ 134 struct nouveau_pci_softc *const sc = device_private(self); 135 const struct pci_attach_args *const pa = aux; 136 uint64_t devname; 137 int error; 138 139 pci_aprint_devinfo(pa, NULL); 140 141 sc->sc_dev = self; 142 143 if (!pmf_device_register(self, &nouveau_pci_suspend, 144 &nouveau_pci_resume)) 145 aprint_error_dev(self, "unable to establish power handler\n"); 146 147 sc->sc_task_state = NOUVEAU_TASK_ATTACH; 148 SIMPLEQ_INIT(&sc->sc_task_u.attach); 149 150 devname = (uint64_t)device_unit(device_parent(self)) << 32; 151 devname |= pa->pa_bus << 16; 152 /* XXX errno Linux->NetBSD */ 153 error = -nvkm_device_pci_new(&sc->sc_pci_dev, 154 nouveau_config, nouveau_debug, true, true, ~0ULL, 155 &sc->sc_nv_dev); 156 if (error) { 157 aprint_error_dev(self, "unable to create nouveau device: %d\n", 158 error); 159 return; 160 } 161 162 /* XXX errno Linux->NetBSD */ 163 error = -drm_pci_attach(self, pa, &sc->sc_pci_dev, nouveau_drm_driver, 164 0, &sc->sc_drm_dev); 165 if (error) { 166 aprint_error_dev(self, "unable to attach drm: %d\n", error); 167 return; 168 } 169 170 while (!SIMPLEQ_EMPTY(&sc->sc_task_u.attach)) { 171 struct nouveau_pci_task *const task = 172 SIMPLEQ_FIRST(&sc->sc_task_u.attach); 173 174 SIMPLEQ_REMOVE_HEAD(&sc->sc_task_u.attach, nt_u.queue); 175 (*task->nt_fn)(task); 176 } 177 178 sc->sc_task_state = NOUVEAU_TASK_WORKQUEUE; 179 error = workqueue_create(&sc->sc_task_u.workqueue, "nouveau_pci", 180 &nouveau_pci_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE); 181 if (error) { 182 aprint_error_dev(self, "unable to create workqueue: %d\n", 183 error); 184 sc->sc_task_u.workqueue = NULL; 185 return; 186 } 187} 188 189static int 190nouveau_pci_detach(device_t self, int flags) 191{ 192 struct nouveau_pci_softc *const sc = device_private(self); 193 int error; 194 195 /* XXX Check for in-use before tearing it all down... */ 196 error = config_detach_children(self, flags); 197 if (error) 198 return error; 199 200 if (sc->sc_task_state == NOUVEAU_TASK_ATTACH) 201 goto out0; 202 if (sc->sc_task_u.workqueue != NULL) { 203 workqueue_destroy(sc->sc_task_u.workqueue); 204 sc->sc_task_u.workqueue = NULL; 205 } 206 207 if (sc->sc_nv_dev == NULL) 208 goto out0; 209 210 if (sc->sc_drm_dev == NULL) 211 goto out1; 212 /* XXX errno Linux->NetBSD */ 213 error = -drm_pci_detach(sc->sc_drm_dev, flags); 214 if (error) 215 /* XXX Kinda too late to fail now... */ 216 return error; 217 sc->sc_drm_dev = NULL; 218 219out1: nvkm_device_del(&sc->sc_nv_dev); 220out0: pmf_device_deregister(self); 221 return 0; 222} 223 224/* 225 * XXX Synchronize with nouveau_do_suspend in nouveau_drm.c. 226 */ 227static bool 228nouveau_pci_suspend(device_t self, const pmf_qual_t *qual __unused) 229{ 230 struct nouveau_pci_softc *const sc = device_private(self); 231 232 return nouveau_pmops_suspend(sc->sc_drm_dev) == 0; 233} 234 235static bool 236nouveau_pci_resume(device_t self, const pmf_qual_t *qual) 237{ 238 struct nouveau_pci_softc *const sc = device_private(self); 239 240 return nouveau_pmops_resume(sc->sc_drm_dev) == 0; 241} 242 243static void 244nouveau_pci_task_work(struct work *work, void *cookie __unused) 245{ 246 struct nouveau_pci_task *const task = container_of(work, 247 struct nouveau_pci_task, nt_u.work); 248 249 (*task->nt_fn)(task); 250} 251 252int 253nouveau_pci_task_schedule(device_t self, struct nouveau_pci_task *task) 254{ 255 struct nouveau_pci_softc *const sc = device_private(self); 256 257 switch (sc->sc_task_state) { 258 case NOUVEAU_TASK_ATTACH: 259 SIMPLEQ_INSERT_TAIL(&sc->sc_task_u.attach, task, nt_u.queue); 260 return 0; 261 case NOUVEAU_TASK_WORKQUEUE: 262 if (sc->sc_task_u.workqueue == NULL) { 263 aprint_error_dev(self, "unable to schedule task\n"); 264 return EIO; 265 } 266 workqueue_enqueue(sc->sc_task_u.workqueue, &task->nt_u.work, 267 NULL); 268 return 0; 269 default: 270 panic("nouveau in invalid task state: %d\n", 271 (int)sc->sc_task_state); 272 } 273} 274 275static int 276nouveau_pci_modcmd(modcmd_t cmd, void *arg __unused) 277{ 278 int error; 279 280 switch (cmd) { 281 case MODULE_CMD_INIT: 282 error = drm_pci_init(nouveau_drm_driver, NULL); 283 if (error) { 284 aprint_error("nouveau_pci: failed to init: %d\n", 285 error); 286 return error; 287 } 288#if 0 /* XXX nouveau acpi */ 289 nouveau_register_dsm_handler(); 290#endif 291 break; 292 case MODULE_CMD_FINI: 293#if 0 /* XXX nouveau acpi */ 294 nouveau_unregister_dsm_handler(); 295#endif 296 drm_pci_exit(nouveau_drm_driver, NULL); 297 break; 298 default: 299 return ENOTTY; 300 } 301 302 return 0; 303} 304