tegra_nouveau.c revision 1.4
1/* $NetBSD: tegra_nouveau.c,v 1.4 2015/10/18 14:20:22 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.4 2015/10/18 14:20:22 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->platformdev = &sc->sc_platform_dev;
142
143	dev->platformdev->id = -1;
144	dev->platformdev->dev = *sc->sc_dev;	/* XXX */
145	dev->platformdev->dmat = sc->sc_dmat;
146	dev->platformdev->nresource = 2;
147	dev->platformdev->resource[0].tag = bst;
148	dev->platformdev->resource[0].start = TEGRA_GPU_BASE;
149	dev->platformdev->resource[0].len = 0x01000000;
150	dev->platformdev->resource[1].tag = bst;
151	dev->platformdev->resource[1].start = TEGRA_GPU_BASE +
152	    dev->platformdev->resource[0].len;
153	dev->platformdev->resource[1].len = 0x01000000;
154
155	error = -drm_dev_register(dev, 0);
156	if (error) {
157		drm_dev_unref(dev);
158		aprint_error_dev(self, "couldn't register DRM device: %d\n",
159		    error);
160		return;
161	}
162
163	aprint_normal_dev(self, "initialized %s %d.%d.%d %s on minor %d\n",
164	    driver->name, driver->major, driver->minor, driver->patchlevel,
165	    driver->date, dev->primary->index);
166}
167
168static int
169tegra_nouveau_get_irq(struct drm_device *dev)
170{
171	return TEGRA_INTR_GPU;
172}
173
174static const char *tegra_nouveau_get_name(struct drm_device *dev)
175{
176	return "tegra_nouveau";
177}
178
179static int
180tegra_nouveau_set_busid(struct drm_device *dev, struct drm_master *master)
181{
182	int id;
183
184	id = dev->platformdev->id;
185	if (id < 0)
186		id = 0;
187
188	master->unique = kmem_asprintf("platform:tegra_nouveau:%02d", id);
189	if (master->unique == NULL)
190		return -ENOMEM;
191	master->unique_len = strlen(master->unique);
192
193	return 0;
194}
195
196static int
197tegra_nouveau_irq_install(struct drm_device *dev,
198    irqreturn_t (*handler)(void *), int flags, const char *name, void *arg,
199    struct drm_bus_irq_cookie **cookiep)
200{
201	void *ih;
202	int irq = TEGRA_INTR_GPU;
203
204	ih = intr_establish(irq, IPL_DRM, IST_LEVEL, handler, arg);
205	if (ih == NULL) {
206		aprint_error_dev(dev->dev,
207		    "couldn't establish interrupt (%s)\n", name);
208		return -ENOENT;
209	}
210
211	aprint_normal_dev(dev->dev, "interrupting on irq %d (%s)\n",
212	    irq, name);
213	*cookiep = (struct drm_bus_irq_cookie *)ih;
214	return 0;
215}
216
217static void
218tegra_nouveau_irq_uninstall(struct drm_device *dev,
219    struct drm_bus_irq_cookie *cookie)
220{
221	intr_disestablish(cookie);
222}
223