drm_pci.c revision 1.18.2.1 1 /* $NetBSD: drm_pci.c,v 1.18.2.1 2018/09/06 06:56:37 pgoyette Exp $ */
2
3 /*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: drm_pci.c,v 1.18.2.1 2018/09/06 06:56:37 pgoyette Exp $");
34
35 #include <sys/types.h>
36 #include <sys/errno.h>
37 #include <sys/systm.h>
38
39 #include <dev/pci/pcivar.h>
40
41 #include <drm/drmP.h>
42 #include <drm/drm_legacy.h>
43
44 struct drm_bus_irq_cookie {
45 pci_intr_handle_t *intr_handles;
46 void *ih_cookie;
47 };
48
49 static const struct pci_attach_args *
50 drm_pci_attach_args(struct drm_device *dev)
51 {
52 return &dev->pdev->pd_pa;
53 }
54
55 int
56 drm_pci_attach(device_t self, const struct pci_attach_args *pa,
57 struct pci_dev *pdev, struct drm_driver *driver, unsigned long cookie,
58 struct drm_device **devp)
59 {
60 struct drm_device *dev;
61 unsigned int unit;
62 int ret;
63
64 /* Ensure the drm agp hooks are initialized. */
65 /* XXX errno NetBSD->Linux */
66 ret = -drm_guarantee_initialized();
67 if (ret)
68 goto fail0;
69
70 /* Create a DRM device. */
71 dev = drm_dev_alloc(driver, self);
72 if (dev == NULL) {
73 ret = -ENOMEM;
74 goto fail0;
75 }
76
77 dev->pdev = pdev;
78 pdev->pd_drm_dev = dev; /* XXX Nouveau kludge. */
79
80 /* XXX Set the power state to D0? */
81
82 /* Set up the bus space and bus DMA tags. */
83 dev->bst = pa->pa_memt;
84 dev->bus_dmat = (pci_dma64_available(pa)? pa->pa_dmat64 : pa->pa_dmat);
85 dev->bus_dmat32 = pa->pa_dmat;
86 dev->dmat = dev->bus_dmat;
87 dev->dmat_subregion_p = false;
88 dev->dmat_subregion_min = 0;
89 dev->dmat_subregion_max = __type_max(bus_addr_t);
90
91 /* Find all the memory maps. */
92 CTASSERT(PCI_NUM_RESOURCES < (SIZE_MAX / sizeof(dev->bus_maps[0])));
93 dev->bus_maps = kmem_zalloc(PCI_NUM_RESOURCES *
94 sizeof(dev->bus_maps[0]), KM_SLEEP);
95 dev->bus_nmaps = PCI_NUM_RESOURCES;
96 for (unit = 0; unit < PCI_NUM_RESOURCES; unit++) {
97 struct drm_bus_map *const bm = &dev->bus_maps[unit];
98 const int reg = PCI_BAR(unit);
99 const pcireg_t type =
100 pci_mapreg_type(pa->pa_pc, pa->pa_tag, reg);
101
102 /* Reject non-memory mappings. */
103 if ((type & PCI_MAPREG_TYPE_MEM) != PCI_MAPREG_TYPE_MEM) {
104 aprint_debug_dev(self, "map %u has non-memory type:"
105 " 0x%"PRIxMAX"\n", unit, (uintmax_t)type);
106 continue;
107 }
108
109 /* Inquire about it. We'll map it in drm_legacy_ioremap. */
110 if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, reg, type,
111 &bm->bm_base, &bm->bm_size, &bm->bm_flags) != 0) {
112 aprint_debug_dev(self, "map %u failed\n", unit);
113 continue;
114 }
115
116 /* Assume since it is a memory mapping it can be linear. */
117 bm->bm_flags |= BUS_SPACE_MAP_LINEAR;
118 }
119
120 /* Set up AGP stuff if requested. */
121 if (drm_core_check_feature(dev, DRIVER_USE_AGP)) {
122 if (drm_pci_device_is_agp(dev))
123 dev->agp = drm_agp_init(dev);
124 if (dev->agp)
125 dev->agp->agp_mtrr = arch_phys_wc_add(dev->agp->base,
126 dev->agp->agp_info.aki_info.ai_aperture_size);
127 }
128
129 /* Register the DRM device and do driver-specific initialization. */
130 ret = drm_dev_register(dev, cookie);
131 if (ret)
132 goto fail1;
133
134 /* Success! */
135 *devp = dev;
136 return 0;
137
138 fail2: __unused
139 drm_dev_unregister(dev);
140 fail1: drm_pci_agp_destroy(dev);
141 dev->bus_nmaps = 0;
142 kmem_free(dev->bus_maps, PCI_NUM_RESOURCES * sizeof(dev->bus_maps[0]));
143 if (dev->dmat_subregion_p) {
144 bus_dmatag_destroy(dev->dmat);
145 }
146 drm_dev_unref(dev);
147 fail0: return ret;
148 }
149
150 int
151 drm_pci_detach(struct drm_device *dev, int flags __unused)
152 {
153
154 /* Do driver-specific detachment and unregister the device. */
155 drm_dev_unregister(dev);
156
157 /* Tear down AGP stuff if necessary. */
158 drm_pci_agp_destroy(dev);
159
160 /* Free the record of available bus space mappings. */
161 dev->bus_nmaps = 0;
162 kmem_free(dev->bus_maps, PCI_NUM_RESOURCES * sizeof(dev->bus_maps[0]));
163
164 /* Tear down bus space and bus DMA tags. */
165 if (dev->dmat_subregion_p) {
166 bus_dmatag_destroy(dev->dmat);
167 }
168
169 drm_dev_unref(dev);
170
171 return 0;
172 }
173
174 void
175 drm_pci_agp_destroy(struct drm_device *dev)
176 {
177
178 if (dev->agp) {
179 arch_phys_wc_del(dev->agp->agp_mtrr);
180 drm_agp_fini(dev);
181 KASSERT(dev->agp == NULL);
182 }
183 }
184
185 int
186 drm_pci_request_irq(struct drm_device *dev, int flags)
187 {
188 const char *const name = device_xname(dev->dev);
189 int (*const handler)(void *) = dev->driver->irq_handler;
190 const struct pci_attach_args *const pa = drm_pci_attach_args(dev);
191 const char *intrstr;
192 char intrbuf[PCI_INTRSTR_LEN];
193 struct drm_bus_irq_cookie *irq_cookie;
194
195 irq_cookie = kmem_alloc(sizeof(*irq_cookie), KM_SLEEP);
196
197 if (dev->pdev->msi_enabled) {
198 if (dev->pdev->pd_intr_handles == NULL) {
199 if (pci_msi_alloc_exact(pa, &irq_cookie->intr_handles,
200 1)) {
201 aprint_error_dev(dev->dev,
202 "couldn't allocate MSI (%s)\n", name);
203 goto error;
204 }
205 } else {
206 irq_cookie->intr_handles = dev->pdev->pd_intr_handles;
207 dev->pdev->pd_intr_handles = NULL;
208 }
209 } else {
210 if (pci_intx_alloc(pa, &irq_cookie->intr_handles)) {
211 aprint_error_dev(dev->dev,
212 "couldn't allocate INTx interrupt (%s)\n", name);
213 goto error;
214 }
215 }
216
217 intrstr = pci_intr_string(pa->pa_pc, irq_cookie->intr_handles[0],
218 intrbuf, sizeof(intrbuf));
219 irq_cookie->ih_cookie = pci_intr_establish_xname(pa->pa_pc,
220 irq_cookie->intr_handles[0], IPL_DRM, handler, dev, name);
221 if (irq_cookie->ih_cookie == NULL) {
222 aprint_error_dev(dev->dev,
223 "couldn't establish interrupt at %s (%s)\n", intrstr, name);
224 pci_intr_release(pa->pa_pc, irq_cookie->intr_handles, 1);
225 goto error;
226 }
227
228 aprint_normal_dev(dev->dev, "interrupting at %s (%s)\n", intrstr, name);
229 dev->irq_cookie = irq_cookie;
230 return 0;
231
232 error:
233 kmem_free(irq_cookie, sizeof(*irq_cookie));
234 return -ENOENT;
235 }
236
237 void
238 drm_pci_free_irq(struct drm_device *dev)
239 {
240 struct drm_bus_irq_cookie *const cookie = dev->irq_cookie;
241 const struct pci_attach_args *pa = drm_pci_attach_args(dev);
242
243 pci_intr_disestablish(pa->pa_pc, cookie->ih_cookie);
244 pci_intr_release(pa->pa_pc, cookie->intr_handles, 1);
245 kmem_free(cookie, sizeof(*cookie));
246 dev->irq_cookie = NULL;
247 }
248
249 int
250 drm_pci_set_busid(struct drm_device *dev, struct drm_master *master)
251 {
252 const struct pci_attach_args *const pa = &dev->pdev->pd_pa;
253
254 master->unique = kasprintf(GFP_KERNEL, "pci:%04x:%02x:%02x.%d",
255 device_unit(device_parent(dev->dev)),
256 pa->pa_bus, pa->pa_device, pa->pa_function);
257 if (master->unique == NULL)
258 return -ENOMEM;
259 master->unique_len = strlen(master->unique);
260
261 return 0;
262 }
263
264 int
265 drm_pci_set_unique(struct drm_device *dev, struct drm_master *master,
266 struct drm_unique *unique)
267 {
268 char kbuf[64], ubuf[64];
269 int ret;
270
271 /* Reject excessively long unique strings. */
272 if (unique->unique_len > sizeof(ubuf) - 1)
273 return -EINVAL;
274
275 /* Copy in the alleged unique string, NUL-terminated. */
276 ret = -copyin(unique->unique, ubuf, unique->unique_len);
277 if (ret)
278 return ret;
279 ubuf[unique->unique_len] = '\0';
280
281 /* Make sure it matches what we expect. */
282 snprintf(kbuf, sizeof kbuf, "PCI:%d:%ld:%ld", dev->pdev->bus->number,
283 (long)PCI_SLOT(dev->pdev->devfn),
284 (long)PCI_FUNC(dev->pdev->devfn));
285 if (strncmp(kbuf, ubuf, sizeof(kbuf)) != 0)
286 return -EINVAL;
287
288 /* Remember it. */
289 master->unique = kstrdup(ubuf, GFP_KERNEL);
290 master->unique_len = strlen(master->unique);
291
292 /* Success! */
293 return 0;
294 }
295