tegra_nouveau.c revision 1.7
1/* $NetBSD: tegra_nouveau.c,v 1.7 2015/10/27 13:21:19 riastradh 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.7 2015/10/27 13:21:19 riastradh 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	prop_dictionary_t prop = device_properties(self);
103	int error;
104
105	sc->sc_dev = self;
106	sc->sc_dmat = tio->tio_dmat;
107
108	aprint_naive("\n");
109	aprint_normal(": GPU\n");
110
111	prop_dictionary_get_cstring(prop, "debug", &nouveau_debug);
112	prop_dictionary_get_cstring(prop, "config", &nouveau_config);
113
114	tegra_car_gpu_enable();
115
116	error = -nouveau_device_create(&sc->sc_platform_dev,
117	    NOUVEAU_BUS_PLATFORM, -1, device_xname(self),
118	    nouveau_config, nouveau_debug, &sc->sc_nv_dev);
119	if (error) {
120		aprint_error_dev(self, "couldn't create nouveau device: %d\n",
121		    error);
122		return;
123	}
124
125	config_mountroot(self, tegra_nouveau_init);
126}
127
128static void
129tegra_nouveau_init(device_t self)
130{
131	struct tegra_nouveau_softc * const sc = device_private(self);
132	struct drm_driver * const driver = nouveau_drm_driver;
133	struct drm_device *dev;
134	bus_space_tag_t bst = &armv7_generic_bs_tag;
135	int error;
136
137	driver->kdriver.platform_device = &sc->sc_platform_dev;
138	driver->bus = &drm_tegra_nouveau_bus;
139
140	dev = drm_dev_alloc(driver, sc->sc_dev);
141	if (dev == NULL) {
142		aprint_error_dev(self, "couldn't allocate DRM device\n");
143		return;
144	}
145	dev->bst = bst;
146	dev->bus_dmat = sc->sc_dmat;
147	dev->dmat = dev->bus_dmat;
148	dev->dmat_subregion_p = false;
149	dev->platformdev = &sc->sc_platform_dev;
150
151	dev->platformdev->id = -1;
152	dev->platformdev->pd_dev = sc->sc_dev;
153	dev->platformdev->dmat = sc->sc_dmat;
154	dev->platformdev->nresource = 2;
155	dev->platformdev->resource[0].tag = bst;
156	dev->platformdev->resource[0].start = TEGRA_GPU_BASE;
157	dev->platformdev->resource[0].len = 0x01000000;
158	dev->platformdev->resource[1].tag = bst;
159	dev->platformdev->resource[1].start = TEGRA_GPU_BASE +
160	    dev->platformdev->resource[0].len;
161	dev->platformdev->resource[1].len = 0x01000000;
162
163	error = -drm_dev_register(dev, 0);
164	if (error) {
165		drm_dev_unref(dev);
166		aprint_error_dev(self, "couldn't register DRM device: %d\n",
167		    error);
168		return;
169	}
170
171	aprint_normal_dev(self, "initialized %s %d.%d.%d %s on minor %d\n",
172	    driver->name, driver->major, driver->minor, driver->patchlevel,
173	    driver->date, dev->primary->index);
174}
175
176static int
177tegra_nouveau_get_irq(struct drm_device *dev)
178{
179	return TEGRA_INTR_GPU;
180}
181
182static const char *tegra_nouveau_get_name(struct drm_device *dev)
183{
184	return "tegra_nouveau";
185}
186
187static int
188tegra_nouveau_set_busid(struct drm_device *dev, struct drm_master *master)
189{
190	int id;
191
192	id = dev->platformdev->id;
193	if (id < 0)
194		id = 0;
195
196	master->unique = kmem_asprintf("platform:tegra_nouveau:%02d", id);
197	if (master->unique == NULL)
198		return -ENOMEM;
199	master->unique_len = strlen(master->unique);
200
201	return 0;
202}
203
204static int
205tegra_nouveau_irq_install(struct drm_device *dev,
206    irqreturn_t (*handler)(void *), int flags, const char *name, void *arg,
207    struct drm_bus_irq_cookie **cookiep)
208{
209	void *ih;
210	int irq = TEGRA_INTR_GPU;
211
212	ih = intr_establish(irq, IPL_DRM, IST_LEVEL, handler, arg);
213	if (ih == NULL) {
214		aprint_error_dev(dev->dev,
215		    "couldn't establish interrupt (%s)\n", name);
216		return -ENOENT;
217	}
218
219	aprint_normal_dev(dev->dev, "interrupting on irq %d (%s)\n",
220	    irq, name);
221	*cookiep = (struct drm_bus_irq_cookie *)ih;
222	return 0;
223}
224
225static void
226tegra_nouveau_irq_uninstall(struct drm_device *dev,
227    struct drm_bus_irq_cookie *cookie)
228{
229	intr_disestablish(cookie);
230}
231