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