xref: /netbsd/sys/arch/arm/nvidia/tegra_nouveau.c (revision 80cbcbc9)
1 /* $NetBSD: tegra_nouveau.c,v 1.2 2015/10/18 00:39:14 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.2 2015/10/18 00:39:14 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 
48 extern char *nouveau_config;
49 extern char *nouveau_debug;
50 extern struct drm_driver *const nouveau_drm_driver;
51 
52 static int	tegra_nouveau_match(device_t, cfdata_t, void *);
53 static void	tegra_nouveau_attach(device_t, device_t, void *);
54 
55 struct tegra_nouveau_softc {
56 	device_t		sc_dev;
57 	struct drm_device	*sc_drm_dev;
58 	struct platform_device	sc_platform_dev;
59 	struct nouveau_device	*sc_nv_dev;
60 };
61 
62 static int	tegra_nouveau_init(struct tegra_nouveau_softc *);
63 
64 static int	tegra_nouveau_get_irq(struct drm_device *);
65 static const char *tegra_nouveau_get_name(struct drm_device *);
66 static int	tegra_nouveau_set_busid(struct drm_device *,
67 					struct drm_master *);
68 static int	tegra_nouveau_irq_install(struct drm_device *,
69 					  irqreturn_t (*)(void *),
70 					  int, const char *, void *,
71 					  struct drm_bus_irq_cookie **);
72 static void	tegra_nouveau_irq_uninstall(struct drm_device *,
73 					    struct drm_bus_irq_cookie *);
74 
75 static struct drm_bus drm_tegra_nouveau_bus = {
76 	.bus_type = DRIVER_BUS_PLATFORM,
77 	.get_irq = tegra_nouveau_get_irq,
78 	.get_name = tegra_nouveau_get_name,
79 	.set_busid = tegra_nouveau_set_busid,
80 	.irq_install = tegra_nouveau_irq_install,
81 	.irq_uninstall = tegra_nouveau_irq_uninstall
82 };
83 
84 CFATTACH_DECL_NEW(tegra_nouveau, sizeof(struct tegra_nouveau_softc),
85 	tegra_nouveau_match, tegra_nouveau_attach, NULL, NULL);
86 
87 static int
88 tegra_nouveau_match(device_t parent, cfdata_t cf, void *aux)
89 {
90 	return 1;
91 }
92 
93 static void
94 tegra_nouveau_attach(device_t parent, device_t self, void *aux)
95 {
96 	struct tegra_nouveau_softc * const sc = device_private(self);
97 #if notyet
98 	struct tegraio_attach_args * const tio = aux;
99 	const struct tegra_locators * const loc = &tio->tio_loc;
100 #endif
101 	int error;
102 
103 	sc->sc_dev = self;
104 #if notyet
105 	sc->sc_bst = tio->tio_bst;
106 	bus_space_subregion(tio->tio_bst, tio->tio_bsh,
107 	    loc->loc_offset, loc->loc_size, &sc->sc_bsh);
108 #endif
109 
110 	aprint_naive("\n");
111 	aprint_normal(": GPU\n");
112 
113 	tegra_car_gpu_enable();
114 
115 	error = -nouveau_device_create(&sc->sc_platform_dev,
116 	    NOUVEAU_BUS_PLATFORM, -1, device_xname(self),
117 	    nouveau_config, nouveau_debug, &sc->sc_nv_dev);
118 	if (error) {
119 		aprint_error_dev(self, "couldn't create nouveau device: %d\n",
120 		    error);
121 		return;
122 	}
123 
124 	error = tegra_nouveau_init(sc);
125 	if (error) {
126 		aprint_error_dev(self, "couldn't attach drm: %d\n", error);
127 		return;
128 	}
129 }
130 
131 static int
132 tegra_nouveau_init(struct tegra_nouveau_softc *sc)
133 {
134 	struct drm_driver * const driver = nouveau_drm_driver;
135 	struct drm_device *dev;
136 	bus_space_tag_t bst = &armv7_generic_bs_tag;
137 	int error;
138 
139 	driver->kdriver.platform_device = &sc->sc_platform_dev;
140 	driver->bus = &drm_tegra_nouveau_bus;
141 
142 	dev = drm_dev_alloc(driver, sc->sc_dev);
143 	if (dev == NULL)
144 		return ENOMEM;
145 	dev->platformdev = &sc->sc_platform_dev;
146 
147 	dev->platformdev->id = -1;
148 	dev->platformdev->nresource = 2;
149 	dev->platformdev->resource[0].tag = bst;
150 	dev->platformdev->resource[0].start = TEGRA_GPU_BASE;
151 	dev->platformdev->resource[0].len = 0x01000000;
152 	dev->platformdev->resource[1].tag = bst;
153 	dev->platformdev->resource[1].start = TEGRA_GPU_BASE +
154 	    dev->platformdev->resource[0].len;
155 	dev->platformdev->resource[1].len = 0x01000000;
156 
157 	error = -drm_dev_register(dev, 0);
158 	if (error) {
159 		drm_dev_unref(dev);
160 		return error;
161 	}
162 
163 	device_printf(sc->sc_dev, "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 	return 0;
168 }
169 
170 static int
171 tegra_nouveau_get_irq(struct drm_device *dev)
172 {
173 	return TEGRA_INTR_GPU;
174 }
175 
176 static const char *tegra_nouveau_get_name(struct drm_device *dev)
177 {
178 	return "tegra_nouveau";
179 }
180 
181 static int
182 tegra_nouveau_set_busid(struct drm_device *dev, struct drm_master *master)
183 {
184 	int id;
185 
186 	id = dev->platformdev->id;
187 	if (id < 0)
188 		id = 0;
189 
190 	master->unique = kmem_asprintf("platform:tegra_nouveau:%02d", id);
191 	if (master->unique == NULL)
192 		return -ENOMEM;
193 	master->unique_len = strlen(master->unique);
194 
195 	return 0;
196 }
197 
198 static int
199 tegra_nouveau_irq_install(struct drm_device *dev,
200     irqreturn_t (*handler)(void *), int flags, const char *name, void *arg,
201     struct drm_bus_irq_cookie **cookiep)
202 {
203 	void *ih;
204 	int irq = TEGRA_INTR_GPU;
205 
206 	ih = intr_establish(irq, IPL_DRM, IST_LEVEL, handler, arg);
207 	if (ih == NULL) {
208 		aprint_error_dev(dev->dev,
209 		    "couldn't establish interrupt (%s)\n", name);
210 		return -ENOENT;
211 	}
212 
213 	aprint_normal_dev(dev->dev, "interrupting on irq %d (%s)\n",
214 	    irq, name);
215 	*cookiep = (struct drm_bus_irq_cookie *)ih;
216 	return 0;
217 }
218 
219 static void
220 tegra_nouveau_irq_uninstall(struct drm_device *dev,
221     struct drm_bus_irq_cookie *cookie)
222 {
223 	intr_disestablish(cookie);
224 }
225