nouveau_pci.c revision 1.3
1/* $NetBSD: nouveau_pci.c,v 1.3 2015/03/06 15:39:28 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.3 2015/03/06 15:39:28 riastradh Exp $"); 34 35#include <sys/types.h> 36#include <sys/device.h> 37#include <sys/queue.h> 38#include <sys/workqueue.h> 39 40#include <drm/drmP.h> 41 42#include <engine/device.h> 43 44#include "nouveau_drm.h" 45#include "nouveau_pci.h" 46 47SIMPLEQ_HEAD(nouveau_task_head, nouveau_task); 48 49struct nouveau_softc { 50 device_t sc_dev; 51 enum { 52 NOUVEAU_TASK_ATTACH, 53 NOUVEAU_TASK_WORKQUEUE, 54 } sc_task_state; 55 union { 56 struct workqueue *workqueue; 57 struct nouveau_task_head attach; 58 } sc_task_u; 59 struct drm_device *sc_drm_dev; 60 struct pci_dev sc_pci_dev; 61 struct nouveau_device *sc_nv_dev; 62}; 63 64static int nouveau_match(device_t, cfdata_t, void *); 65static void nouveau_attach(device_t, device_t, void *); 66static int nouveau_detach(device_t, int); 67 68static bool nouveau_suspend(device_t, const pmf_qual_t *); 69static bool nouveau_resume(device_t, const pmf_qual_t *); 70 71static void nouveau_task_work(struct work *, void *); 72 73CFATTACH_DECL_NEW(nouveau, sizeof(struct nouveau_softc), 74 nouveau_match, nouveau_attach, nouveau_detach, NULL); 75 76/* Kludge to get this from nouveau_drm.c. */ 77extern struct drm_driver *const nouveau_drm_driver; 78 79static int 80nouveau_match(device_t parent, cfdata_t match, void *aux) 81{ 82 extern int nouveau_guarantee_initialized(void); 83 const struct pci_attach_args *const pa = aux; 84 int error; 85 86 error = nouveau_guarantee_initialized(); 87 if (error) { 88 aprint_error("nouveau: failed to initialize: %d\n", error); 89 return 0; 90 } 91 92 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA && 93 PCI_VENDOR(pa->pa_id) != PCI_VENDOR_NVIDIA_SGS) 94 return 0; 95 96 if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY) 97 return 0; 98 99 return 6; /* XXX Beat genfb_pci... */ 100} 101 102extern char *nouveau_config; 103extern char *nouveau_debug; 104 105static void 106nouveau_attach(device_t parent, device_t self, void *aux) 107{ 108 struct nouveau_softc *const sc = device_private(self); 109 const struct pci_attach_args *const pa = aux; 110 uint64_t devname; 111 int error; 112 113 pci_aprint_devinfo(pa, NULL); 114 115 sc->sc_dev = self; 116 117 if (!pmf_device_register(self, &nouveau_suspend, 118 &nouveau_resume)) 119 aprint_error_dev(self, "unable to establish power handler\n"); 120 121 sc->sc_task_state = NOUVEAU_TASK_ATTACH; 122 SIMPLEQ_INIT(&sc->sc_task_u.attach); 123 124 devname = (uint64_t)device_unit(device_parent(self)) << 32; 125 devname |= pa->pa_bus << 16; 126 /* XXX errno Linux->NetBSD */ 127 error = -nouveau_device_create(&sc->sc_pci_dev, NOUVEAU_BUS_PCI, 128 devname, device_xname(self), nouveau_config, nouveau_debug, 129 &sc->sc_nv_dev); 130 if (error) { 131 aprint_error_dev(self, "unable to create nouveau device: %d\n", 132 error); 133 return; 134 } 135 136 /* XXX errno Linux->NetBSD */ 137 error = -drm_pci_attach(self, pa, &sc->sc_pci_dev, nouveau_drm_driver, 138 0, &sc->sc_drm_dev); 139 if (error) { 140 aprint_error_dev(self, "unable to attach drm: %d\n", error); 141 return; 142 } 143 144 while (!SIMPLEQ_EMPTY(&sc->sc_task_u.attach)) { 145 struct nouveau_task *const task = 146 SIMPLEQ_FIRST(&sc->sc_task_u.attach); 147 148 SIMPLEQ_REMOVE_HEAD(&sc->sc_task_u.attach, nt_u.queue); 149 (*task->nt_fn)(task); 150 } 151 152 sc->sc_task_state = NOUVEAU_TASK_WORKQUEUE; 153 error = workqueue_create(&sc->sc_task_u.workqueue, "intelfb", 154 &nouveau_task_work, NULL, PRI_NONE, IPL_NONE, WQ_MPSAFE); 155 if (error) { 156 aprint_error_dev(self, "unable to create workqueue: %d\n", 157 error); 158 sc->sc_task_u.workqueue = NULL; 159 return; 160 } 161} 162 163static int 164nouveau_detach(device_t self, int flags) 165{ 166 struct nouveau_softc *const sc = device_private(self); 167 int error; 168 169 /* XXX Check for in-use before tearing it all down... */ 170 error = config_detach_children(self, flags); 171 if (error) 172 return error; 173 174 if (sc->sc_task_state == NOUVEAU_TASK_ATTACH) 175 goto out0; 176 if (sc->sc_task_u.workqueue != NULL) { 177 workqueue_destroy(sc->sc_task_u.workqueue); 178 sc->sc_task_u.workqueue = NULL; 179 } 180 181 if (sc->sc_nv_dev == NULL) 182 goto out0; 183 184 if (sc->sc_drm_dev == NULL) 185 goto out1; 186 /* XXX errno Linux->NetBSD */ 187 error = -drm_pci_detach(sc->sc_drm_dev, flags); 188 if (error) 189 /* XXX Kinda too late to fail now... */ 190 return error; 191 sc->sc_drm_dev = NULL; 192 193out1: nouveau_object_ref(NULL, (void *)&sc->sc_nv_dev); 194out0: pmf_device_deregister(self); 195 return 0; 196} 197 198/* 199 * XXX Synchronize with nouveau_do_suspend in nouveau_drm.c. 200 */ 201static bool 202nouveau_suspend(device_t self, const pmf_qual_t *qual __unused) 203{ 204 struct nouveau_softc *const sc = device_private(self); 205 struct device *const dev = &sc->sc_pci_dev.dev; /* XXX KLUDGE */ 206 207 return nouveau_pmops_suspend(dev) == 0; 208} 209 210static bool 211nouveau_resume(device_t self, const pmf_qual_t *qual) 212{ 213 struct nouveau_softc *const sc = device_private(self); 214 struct device *const dev = &sc->sc_pci_dev.dev; /* XXX KLUDGE */ 215 216 return nouveau_pmops_resume(dev) == 0; 217} 218 219static void 220nouveau_task_work(struct work *work, void *cookie __unused) 221{ 222 struct nouveau_task *const task = container_of(work, 223 struct nouveau_task, nt_u.work); 224 225 (*task->nt_fn)(task); 226} 227 228int 229nouveau_task_schedule(device_t self, struct nouveau_task *task) 230{ 231 struct nouveau_softc *const sc = device_private(self); 232 233 switch (sc->sc_task_state) { 234 case NOUVEAU_TASK_ATTACH: 235 SIMPLEQ_INSERT_TAIL(&sc->sc_task_u.attach, task, nt_u.queue); 236 return 0; 237 case NOUVEAU_TASK_WORKQUEUE: 238 if (sc->sc_task_u.workqueue == NULL) { 239 aprint_error_dev(self, "unable to schedule task\n"); 240 return EIO; 241 } 242 workqueue_enqueue(sc->sc_task_u.workqueue, &task->nt_u.work, 243 NULL); 244 return 0; 245 default: 246 panic("nouveau in invalid task state: %d\n", 247 (int)sc->sc_task_state); 248 } 249} 250