drm_pci.c revision 1.19 1 1.19 riastrad /* $NetBSD: drm_pci.c,v 1.19 2018/08/27 06:06:42 riastradh Exp $ */
2 1.2 riastrad
3 1.2 riastrad /*-
4 1.2 riastrad * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.2 riastrad * All rights reserved.
6 1.2 riastrad *
7 1.2 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.2 riastrad * by Taylor R. Campbell.
9 1.2 riastrad *
10 1.2 riastrad * Redistribution and use in source and binary forms, with or without
11 1.2 riastrad * modification, are permitted provided that the following conditions
12 1.2 riastrad * are met:
13 1.2 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.2 riastrad * notice, this list of conditions and the following disclaimer.
15 1.2 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.2 riastrad * documentation and/or other materials provided with the distribution.
18 1.2 riastrad *
19 1.2 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.2 riastrad */
31 1.2 riastrad
32 1.2 riastrad #include <sys/cdefs.h>
33 1.19 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_pci.c,v 1.19 2018/08/27 06:06:42 riastradh Exp $");
34 1.2 riastrad
35 1.2 riastrad #include <sys/types.h>
36 1.2 riastrad #include <sys/errno.h>
37 1.2 riastrad #include <sys/systm.h>
38 1.2 riastrad
39 1.2 riastrad #include <dev/pci/pcivar.h>
40 1.2 riastrad
41 1.2 riastrad #include <drm/drmP.h>
42 1.2 riastrad
43 1.16 nonaka struct drm_bus_irq_cookie {
44 1.16 nonaka pci_intr_handle_t *intr_handles;
45 1.16 nonaka void *ih_cookie;
46 1.16 nonaka };
47 1.16 nonaka
48 1.2 riastrad static int drm_pci_get_irq(struct drm_device *);
49 1.2 riastrad static int drm_pci_irq_install(struct drm_device *,
50 1.2 riastrad irqreturn_t (*)(void *), int, const char *, void *,
51 1.2 riastrad struct drm_bus_irq_cookie **);
52 1.2 riastrad static void drm_pci_irq_uninstall(struct drm_device *,
53 1.2 riastrad struct drm_bus_irq_cookie *);
54 1.2 riastrad static const char *
55 1.2 riastrad drm_pci_get_name(struct drm_device *);
56 1.2 riastrad static int drm_pci_set_unique(struct drm_device *, struct drm_master *,
57 1.2 riastrad struct drm_unique *);
58 1.2 riastrad static int drm_pci_irq_by_busid(struct drm_device *,
59 1.2 riastrad struct drm_irq_busid *);
60 1.2 riastrad
61 1.2 riastrad const struct drm_bus drm_pci_bus = {
62 1.2 riastrad .bus_type = DRIVER_BUS_PCI,
63 1.2 riastrad .get_irq = drm_pci_get_irq,
64 1.2 riastrad .irq_install = drm_pci_irq_install,
65 1.2 riastrad .irq_uninstall = drm_pci_irq_uninstall,
66 1.2 riastrad .get_name = drm_pci_get_name,
67 1.2 riastrad .set_busid = drm_pci_set_busid,
68 1.2 riastrad .set_unique = drm_pci_set_unique,
69 1.2 riastrad .irq_by_busid = drm_pci_irq_by_busid,
70 1.2 riastrad };
71 1.2 riastrad
72 1.2 riastrad static const struct pci_attach_args *
73 1.2 riastrad drm_pci_attach_args(struct drm_device *dev)
74 1.2 riastrad {
75 1.2 riastrad return &dev->pdev->pd_pa;
76 1.2 riastrad }
77 1.2 riastrad
78 1.2 riastrad int
79 1.2 riastrad drm_pci_init(struct drm_driver *driver, struct pci_driver *pdriver __unused)
80 1.2 riastrad {
81 1.2 riastrad
82 1.2 riastrad driver->bus = &drm_pci_bus;
83 1.2 riastrad return 0;
84 1.2 riastrad }
85 1.2 riastrad
86 1.2 riastrad void
87 1.2 riastrad drm_pci_exit(struct drm_driver *driver __unused,
88 1.2 riastrad struct pci_driver *pdriver __unused)
89 1.2 riastrad {
90 1.2 riastrad }
91 1.2 riastrad
92 1.4 riastrad int
93 1.2 riastrad drm_pci_attach(device_t self, const struct pci_attach_args *pa,
94 1.4 riastrad struct pci_dev *pdev, struct drm_driver *driver, unsigned long cookie,
95 1.4 riastrad struct drm_device **devp)
96 1.2 riastrad {
97 1.4 riastrad struct drm_device *dev;
98 1.2 riastrad unsigned int unit;
99 1.4 riastrad int ret;
100 1.2 riastrad
101 1.10 riastrad /* Ensure the drm agp hooks are installed. */
102 1.10 riastrad /* XXX errno NetBSD->Linux */
103 1.10 riastrad ret = -drmkms_pci_agp_guarantee_initialized();
104 1.10 riastrad if (ret)
105 1.10 riastrad goto fail0;
106 1.10 riastrad
107 1.4 riastrad /* Initialize the Linux PCI device descriptor. */
108 1.2 riastrad linux_pci_dev_init(pdev, self, pa, 0);
109 1.2 riastrad
110 1.4 riastrad /* Create a DRM device. */
111 1.4 riastrad dev = drm_dev_alloc(driver, self);
112 1.4 riastrad if (dev == NULL) {
113 1.4 riastrad ret = -ENOMEM;
114 1.4 riastrad goto fail0;
115 1.4 riastrad }
116 1.4 riastrad
117 1.2 riastrad dev->pdev = pdev;
118 1.11 riastrad pdev->pd_drm_dev = dev; /* XXX Nouveau kludge. */
119 1.2 riastrad
120 1.2 riastrad /* XXX Set the power state to D0? */
121 1.2 riastrad
122 1.4 riastrad /* Set up the bus space and bus DMA tags. */
123 1.2 riastrad dev->bst = pa->pa_memt;
124 1.2 riastrad /* XXX Let the driver say something about 32-bit vs 64-bit DMA? */
125 1.2 riastrad dev->bus_dmat = (pci_dma64_available(pa)? pa->pa_dmat64 : pa->pa_dmat);
126 1.2 riastrad dev->dmat = dev->bus_dmat;
127 1.2 riastrad dev->dmat_subregion_p = false;
128 1.2 riastrad
129 1.4 riastrad /* Find all the memory maps. */
130 1.4 riastrad CTASSERT(PCI_NUM_RESOURCES < (SIZE_MAX / sizeof(dev->bus_maps[0])));
131 1.4 riastrad dev->bus_maps = kmem_zalloc(PCI_NUM_RESOURCES *
132 1.4 riastrad sizeof(dev->bus_maps[0]), KM_SLEEP);
133 1.4 riastrad dev->bus_nmaps = PCI_NUM_RESOURCES;
134 1.4 riastrad for (unit = 0; unit < PCI_NUM_RESOURCES; unit++) {
135 1.2 riastrad struct drm_bus_map *const bm = &dev->bus_maps[unit];
136 1.2 riastrad const int reg = PCI_BAR(unit);
137 1.2 riastrad const pcireg_t type =
138 1.2 riastrad pci_mapreg_type(pa->pa_pc, pa->pa_tag, reg);
139 1.2 riastrad
140 1.2 riastrad /* Reject non-memory mappings. */
141 1.2 riastrad if ((type & PCI_MAPREG_TYPE_MEM) != PCI_MAPREG_TYPE_MEM) {
142 1.2 riastrad aprint_debug_dev(self, "map %u has non-memory type:"
143 1.2 riastrad " 0x%"PRIxMAX"\n", unit, (uintmax_t)type);
144 1.2 riastrad continue;
145 1.2 riastrad }
146 1.2 riastrad
147 1.13 riastrad /* Inquire about it. We'll map it in drm_core_ioremap. */
148 1.2 riastrad if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, reg, type,
149 1.2 riastrad &bm->bm_base, &bm->bm_size, &bm->bm_flags) != 0) {
150 1.2 riastrad aprint_debug_dev(self, "map %u failed\n", unit);
151 1.2 riastrad continue;
152 1.2 riastrad }
153 1.2 riastrad
154 1.2 riastrad /* Assume since it is a memory mapping it can be linear. */
155 1.2 riastrad bm->bm_flags |= BUS_SPACE_MAP_LINEAR;
156 1.2 riastrad }
157 1.2 riastrad
158 1.4 riastrad /* Set up AGP stuff if requested. */
159 1.4 riastrad if (drm_core_check_feature(dev, DRIVER_USE_AGP)) {
160 1.4 riastrad if (drm_pci_device_is_agp(dev))
161 1.4 riastrad dev->agp = drm_agp_init(dev);
162 1.4 riastrad if (dev->agp)
163 1.4 riastrad dev->agp->agp_mtrr = arch_phys_wc_add(dev->agp->base,
164 1.4 riastrad dev->agp->agp_info.aki_info.ai_aperture_size);
165 1.4 riastrad }
166 1.4 riastrad
167 1.4 riastrad /* Register the DRM device and do driver-specific initialization. */
168 1.4 riastrad ret = drm_dev_register(dev, cookie);
169 1.4 riastrad if (ret)
170 1.4 riastrad goto fail1;
171 1.4 riastrad
172 1.4 riastrad /* Success! */
173 1.4 riastrad *devp = dev;
174 1.4 riastrad return 0;
175 1.4 riastrad
176 1.4 riastrad fail2: __unused
177 1.4 riastrad drm_dev_unregister(dev);
178 1.4 riastrad fail1: drm_pci_agp_destroy(dev);
179 1.4 riastrad dev->bus_nmaps = 0;
180 1.4 riastrad kmem_free(dev->bus_maps, PCI_NUM_RESOURCES * sizeof(dev->bus_maps[0]));
181 1.5 riastrad if (dev->dmat_subregion_p)
182 1.5 riastrad bus_dmatag_destroy(dev->dmat);
183 1.4 riastrad drm_dev_unref(dev);
184 1.4 riastrad fail0: return ret;
185 1.2 riastrad }
186 1.2 riastrad
187 1.2 riastrad int
188 1.4 riastrad drm_pci_detach(struct drm_device *dev, int flags __unused)
189 1.2 riastrad {
190 1.2 riastrad
191 1.4 riastrad /* Do driver-specific detachment and unregister the device. */
192 1.4 riastrad drm_dev_unregister(dev);
193 1.4 riastrad
194 1.4 riastrad /* Tear down AGP stuff if necessary. */
195 1.8 riastrad drm_pci_agp_destroy(dev);
196 1.4 riastrad
197 1.4 riastrad /* Free the record of available bus space mappings. */
198 1.4 riastrad dev->bus_nmaps = 0;
199 1.4 riastrad kmem_free(dev->bus_maps, PCI_NUM_RESOURCES * sizeof(dev->bus_maps[0]));
200 1.2 riastrad
201 1.4 riastrad /* Tear down bus space and bus DMA tags. */
202 1.2 riastrad if (dev->dmat_subregion_p)
203 1.2 riastrad bus_dmatag_destroy(dev->dmat);
204 1.2 riastrad
205 1.4 riastrad drm_dev_unref(dev);
206 1.4 riastrad
207 1.2 riastrad return 0;
208 1.2 riastrad }
209 1.2 riastrad
210 1.4 riastrad void
211 1.4 riastrad drm_pci_agp_destroy(struct drm_device *dev)
212 1.4 riastrad {
213 1.4 riastrad
214 1.4 riastrad if (dev->agp) {
215 1.4 riastrad arch_phys_wc_del(dev->agp->agp_mtrr);
216 1.4 riastrad drm_agp_clear(dev);
217 1.4 riastrad kfree(dev->agp); /* XXX Should go in drm_agp_clear... */
218 1.4 riastrad dev->agp = NULL;
219 1.4 riastrad }
220 1.4 riastrad }
221 1.4 riastrad
222 1.2 riastrad static int
223 1.2 riastrad drm_pci_get_irq(struct drm_device *dev)
224 1.2 riastrad {
225 1.2 riastrad
226 1.2 riastrad /*
227 1.12 riastrad * Caller expects a nonzero int, and doesn't really use it for
228 1.12 riastrad * anything, so no need to pci_intr_map here.
229 1.2 riastrad */
230 1.12 riastrad return dev->pdev->pd_pa.pa_intrpin;
231 1.2 riastrad }
232 1.2 riastrad
233 1.2 riastrad static int
234 1.2 riastrad drm_pci_irq_install(struct drm_device *dev, irqreturn_t (*handler)(void *),
235 1.16 nonaka int flags, const char *name, void *arg, struct drm_bus_irq_cookie **cookiep)
236 1.2 riastrad {
237 1.2 riastrad const struct pci_attach_args *const pa = drm_pci_attach_args(dev);
238 1.2 riastrad const char *intrstr;
239 1.3 christos char intrbuf[PCI_INTRSTR_LEN];
240 1.16 nonaka struct drm_bus_irq_cookie *irq_cookie;
241 1.2 riastrad
242 1.16 nonaka irq_cookie = kmem_alloc(sizeof(*irq_cookie), KM_SLEEP);
243 1.16 nonaka
244 1.16 nonaka if (dev->pdev->msi_enabled) {
245 1.18 nonaka if (dev->pdev->intr_handles == NULL) {
246 1.18 nonaka if (pci_msi_alloc_exact(pa, &irq_cookie->intr_handles,
247 1.18 nonaka 1)) {
248 1.18 nonaka aprint_error_dev(dev->dev,
249 1.18 nonaka "couldn't allocate MSI (%s)\n", name);
250 1.18 nonaka goto error;
251 1.18 nonaka }
252 1.18 nonaka } else {
253 1.18 nonaka irq_cookie->intr_handles = dev->pdev->intr_handles;
254 1.18 nonaka dev->pdev->intr_handles = NULL;
255 1.18 nonaka }
256 1.16 nonaka } else {
257 1.18 nonaka if (pci_intx_alloc(pa, &irq_cookie->intr_handles)) {
258 1.18 nonaka aprint_error_dev(dev->dev,
259 1.18 nonaka "couldn't allocate INTx interrupt (%s)\n", name);
260 1.18 nonaka goto error;
261 1.18 nonaka }
262 1.16 nonaka }
263 1.2 riastrad
264 1.16 nonaka intrstr = pci_intr_string(pa->pa_pc, irq_cookie->intr_handles[0],
265 1.16 nonaka intrbuf, sizeof(intrbuf));
266 1.16 nonaka irq_cookie->ih_cookie = pci_intr_establish_xname(pa->pa_pc,
267 1.16 nonaka irq_cookie->intr_handles[0], IPL_DRM, handler, arg, name);
268 1.16 nonaka if (irq_cookie->ih_cookie == NULL) {
269 1.2 riastrad aprint_error_dev(dev->dev,
270 1.16 nonaka "couldn't establish interrupt at %s (%s)\n", intrstr, name);
271 1.18 nonaka pci_intr_release(pa->pa_pc, irq_cookie->intr_handles, 1);
272 1.18 nonaka goto error;
273 1.2 riastrad }
274 1.2 riastrad
275 1.16 nonaka aprint_normal_dev(dev->dev, "interrupting at %s (%s)\n", intrstr, name);
276 1.16 nonaka *cookiep = irq_cookie;
277 1.2 riastrad return 0;
278 1.18 nonaka
279 1.18 nonaka error:
280 1.18 nonaka kmem_free(irq_cookie, sizeof(*irq_cookie));
281 1.18 nonaka return -ENOENT;
282 1.2 riastrad }
283 1.2 riastrad
284 1.2 riastrad static void
285 1.16 nonaka drm_pci_irq_uninstall(struct drm_device *dev, struct drm_bus_irq_cookie *cookie)
286 1.2 riastrad {
287 1.2 riastrad const struct pci_attach_args *pa = drm_pci_attach_args(dev);
288 1.2 riastrad
289 1.16 nonaka pci_intr_disestablish(pa->pa_pc, cookie->ih_cookie);
290 1.16 nonaka pci_intr_release(pa->pa_pc, cookie->intr_handles, 1);
291 1.16 nonaka kmem_free(cookie, sizeof(*cookie));
292 1.2 riastrad }
293 1.2 riastrad
294 1.2 riastrad static const char *
295 1.2 riastrad drm_pci_get_name(struct drm_device *dev)
296 1.2 riastrad {
297 1.2 riastrad return "pci"; /* XXX PCI bus names? */
298 1.2 riastrad }
299 1.2 riastrad
300 1.2 riastrad static int
301 1.2 riastrad drm_pci_format_unique(struct drm_device *dev, char *buf, size_t size)
302 1.2 riastrad {
303 1.6 riastrad const unsigned int domain = device_unit(device_parent(dev->dev));
304 1.2 riastrad const unsigned int bus = dev->pdev->pd_pa.pa_bus;
305 1.2 riastrad const unsigned int device = dev->pdev->pd_pa.pa_device;
306 1.2 riastrad const unsigned int function = dev->pdev->pd_pa.pa_function;
307 1.2 riastrad
308 1.2 riastrad return snprintf(buf, size, "pci:%04x:%02x:%02x.%d",
309 1.2 riastrad domain, bus, device, function);
310 1.2 riastrad }
311 1.2 riastrad
312 1.2 riastrad static int
313 1.2 riastrad drm_pci_format_devname(struct drm_device *dev, const char *unique,
314 1.2 riastrad char *buf, size_t size)
315 1.2 riastrad {
316 1.2 riastrad
317 1.2 riastrad return snprintf(buf, size, "%s@%s",
318 1.2 riastrad device_xname(device_parent(dev->dev)),
319 1.2 riastrad unique);
320 1.2 riastrad }
321 1.2 riastrad
322 1.19 riastrad int
323 1.2 riastrad drm_pci_set_busid(struct drm_device *dev, struct drm_master *master)
324 1.2 riastrad {
325 1.2 riastrad int n;
326 1.2 riastrad char *buf;
327 1.2 riastrad
328 1.2 riastrad n = drm_pci_format_unique(dev, NULL, 0);
329 1.2 riastrad if (n < 0)
330 1.2 riastrad return -ENOSPC; /* XXX */
331 1.2 riastrad if (0xff < n)
332 1.2 riastrad n = 0xff;
333 1.2 riastrad
334 1.2 riastrad buf = kzalloc(n + 1, GFP_KERNEL);
335 1.2 riastrad (void)drm_pci_format_unique(dev, buf, n + 1);
336 1.2 riastrad
337 1.2 riastrad if (master->unique)
338 1.2 riastrad kfree(master->unique);
339 1.2 riastrad master->unique = buf;
340 1.2 riastrad master->unique_len = n;
341 1.2 riastrad master->unique_size = n + 1;
342 1.2 riastrad
343 1.2 riastrad n = drm_pci_format_devname(dev, master->unique, NULL, 0);
344 1.2 riastrad if (n < 0)
345 1.2 riastrad return -ENOSPC; /* XXX back out? */
346 1.2 riastrad if (0xff < n)
347 1.2 riastrad n = 0xff;
348 1.2 riastrad
349 1.2 riastrad buf = kzalloc(n + 1, GFP_KERNEL);
350 1.2 riastrad (void)drm_pci_format_devname(dev, master->unique, buf, n + 1);
351 1.2 riastrad
352 1.2 riastrad if (dev->devname)
353 1.2 riastrad kfree(dev->devname);
354 1.2 riastrad dev->devname = buf;
355 1.2 riastrad
356 1.2 riastrad return 0;
357 1.2 riastrad }
358 1.2 riastrad
359 1.2 riastrad static int
360 1.2 riastrad drm_pci_set_unique(struct drm_device *dev, struct drm_master *master,
361 1.2 riastrad struct drm_unique *unique __unused)
362 1.2 riastrad {
363 1.2 riastrad
364 1.2 riastrad /*
365 1.2 riastrad * XXX This is silly. We're supposed to reject unique names
366 1.2 riastrad * that don't match the ones we would generate anyway. For
367 1.2 riastrad * expedience, we'll just generate the one we would and ignore
368 1.2 riastrad * whatever userland threw at us...
369 1.2 riastrad */
370 1.2 riastrad return drm_pci_set_busid(dev, master);
371 1.2 riastrad }
372 1.2 riastrad
373 1.2 riastrad static int
374 1.2 riastrad drm_pci_irq_by_busid(struct drm_device *dev, struct drm_irq_busid *busid)
375 1.2 riastrad {
376 1.2 riastrad return -ENOSYS; /* XXX */
377 1.2 riastrad }
378