tegra_nouveau.c revision 1.5
1/* $NetBSD: tegra_nouveau.c,v 1.5 2015/10/18 14:31:33 jmcneill Exp $ */ 2 3/*- 4 * Copyright (c) 2015 Jared D. McNeill <jmcneill@invisible.ca> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29#include "locators.h" 30 31#include <sys/cdefs.h> 32__KERNEL_RCSID(0, "$NetBSD: tegra_nouveau.c,v 1.5 2015/10/18 14:31:33 jmcneill Exp $"); 33 34#include <sys/param.h> 35#include <sys/bus.h> 36#include <sys/device.h> 37#include <sys/intr.h> 38#include <sys/systm.h> 39#include <sys/kernel.h> 40#include <sys/module.h> 41 42#include <arm/nvidia/tegra_reg.h> 43#include <arm/nvidia/tegra_var.h> 44 45#include <drm/drmP.h> 46#include <engine/device.h> 47 48extern char *nouveau_config; 49extern char *nouveau_debug; 50extern struct drm_driver *const nouveau_drm_driver; 51 52static int tegra_nouveau_match(device_t, cfdata_t, void *); 53static void tegra_nouveau_attach(device_t, device_t, void *); 54 55struct tegra_nouveau_softc { 56 device_t sc_dev; 57 bus_dma_tag_t sc_dmat; 58 struct drm_device *sc_drm_dev; 59 struct platform_device sc_platform_dev; 60 struct nouveau_device *sc_nv_dev; 61}; 62 63static void tegra_nouveau_init(device_t); 64 65static int tegra_nouveau_get_irq(struct drm_device *); 66static const char *tegra_nouveau_get_name(struct drm_device *); 67static int tegra_nouveau_set_busid(struct drm_device *, 68 struct drm_master *); 69static int tegra_nouveau_irq_install(struct drm_device *, 70 irqreturn_t (*)(void *), 71 int, const char *, void *, 72 struct drm_bus_irq_cookie **); 73static void tegra_nouveau_irq_uninstall(struct drm_device *, 74 struct drm_bus_irq_cookie *); 75 76static struct drm_bus drm_tegra_nouveau_bus = { 77 .bus_type = DRIVER_BUS_PLATFORM, 78 .get_irq = tegra_nouveau_get_irq, 79 .get_name = tegra_nouveau_get_name, 80 .set_busid = tegra_nouveau_set_busid, 81 .irq_install = tegra_nouveau_irq_install, 82 .irq_uninstall = tegra_nouveau_irq_uninstall 83}; 84 85CFATTACH_DECL_NEW(tegra_nouveau, sizeof(struct tegra_nouveau_softc), 86 tegra_nouveau_match, tegra_nouveau_attach, NULL, NULL); 87 88static int 89tegra_nouveau_match(device_t parent, cfdata_t cf, void *aux) 90{ 91 return 1; 92} 93 94static void 95tegra_nouveau_attach(device_t parent, device_t self, void *aux) 96{ 97 struct tegra_nouveau_softc * const sc = device_private(self); 98 struct tegraio_attach_args * const tio = aux; 99#if notyet 100 const struct tegra_locators * const loc = &tio->tio_loc; 101#endif 102 int error; 103 104 sc->sc_dev = self; 105 sc->sc_dmat = tio->tio_dmat; 106 107 aprint_naive("\n"); 108 aprint_normal(": GPU\n"); 109 110 tegra_car_gpu_enable(); 111 112 error = -nouveau_device_create(&sc->sc_platform_dev, 113 NOUVEAU_BUS_PLATFORM, -1, device_xname(self), 114 nouveau_config, nouveau_debug, &sc->sc_nv_dev); 115 if (error) { 116 aprint_error_dev(self, "couldn't create nouveau device: %d\n", 117 error); 118 return; 119 } 120 121 config_mountroot(self, tegra_nouveau_init); 122} 123 124static void 125tegra_nouveau_init(device_t self) 126{ 127 struct tegra_nouveau_softc * const sc = device_private(self); 128 struct drm_driver * const driver = nouveau_drm_driver; 129 struct drm_device *dev; 130 bus_space_tag_t bst = &armv7_generic_bs_tag; 131 int error; 132 133 driver->kdriver.platform_device = &sc->sc_platform_dev; 134 driver->bus = &drm_tegra_nouveau_bus; 135 136 dev = drm_dev_alloc(driver, sc->sc_dev); 137 if (dev == NULL) { 138 aprint_error_dev(self, "couldn't allocate DRM device\n"); 139 return; 140 } 141 dev->bst = bst; 142 dev->bus_dmat = sc->sc_dmat; 143 dev->dmat = dev->bus_dmat; 144 dev->dmat_subregion_p = false; 145 dev->platformdev = &sc->sc_platform_dev; 146 147 dev->platformdev->id = -1; 148 dev->platformdev->dev = *sc->sc_dev; /* XXX */ 149 dev->platformdev->dmat = sc->sc_dmat; 150 dev->platformdev->nresource = 2; 151 dev->platformdev->resource[0].tag = bst; 152 dev->platformdev->resource[0].start = TEGRA_GPU_BASE; 153 dev->platformdev->resource[0].len = 0x01000000; 154 dev->platformdev->resource[1].tag = bst; 155 dev->platformdev->resource[1].start = TEGRA_GPU_BASE + 156 dev->platformdev->resource[0].len; 157 dev->platformdev->resource[1].len = 0x01000000; 158 159 error = -drm_dev_register(dev, 0); 160 if (error) { 161 drm_dev_unref(dev); 162 aprint_error_dev(self, "couldn't register DRM device: %d\n", 163 error); 164 return; 165 } 166 167 aprint_normal_dev(self, "initialized %s %d.%d.%d %s on minor %d\n", 168 driver->name, driver->major, driver->minor, driver->patchlevel, 169 driver->date, dev->primary->index); 170} 171 172static int 173tegra_nouveau_get_irq(struct drm_device *dev) 174{ 175 return TEGRA_INTR_GPU; 176} 177 178static const char *tegra_nouveau_get_name(struct drm_device *dev) 179{ 180 return "tegra_nouveau"; 181} 182 183static int 184tegra_nouveau_set_busid(struct drm_device *dev, struct drm_master *master) 185{ 186 int id; 187 188 id = dev->platformdev->id; 189 if (id < 0) 190 id = 0; 191 192 master->unique = kmem_asprintf("platform:tegra_nouveau:%02d", id); 193 if (master->unique == NULL) 194 return -ENOMEM; 195 master->unique_len = strlen(master->unique); 196 197 return 0; 198} 199 200static int 201tegra_nouveau_irq_install(struct drm_device *dev, 202 irqreturn_t (*handler)(void *), int flags, const char *name, void *arg, 203 struct drm_bus_irq_cookie **cookiep) 204{ 205 void *ih; 206 int irq = TEGRA_INTR_GPU; 207 208 ih = intr_establish(irq, IPL_DRM, IST_LEVEL, handler, arg); 209 if (ih == NULL) { 210 aprint_error_dev(dev->dev, 211 "couldn't establish interrupt (%s)\n", name); 212 return -ENOENT; 213 } 214 215 aprint_normal_dev(dev->dev, "interrupting on irq %d (%s)\n", 216 irq, name); 217 *cookiep = (struct drm_bus_irq_cookie *)ih; 218 return 0; 219} 220 221static void 222tegra_nouveau_irq_uninstall(struct drm_device *dev, 223 struct drm_bus_irq_cookie *cookie) 224{ 225 intr_disestablish(cookie); 226} 227