Home | History | Annotate | Line # | Download | only in pci
drm_pci.c revision 1.16
      1 /*	$NetBSD: drm_pci.c,v 1.16 2017/03/02 04:31:51 nonaka 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.16 2017/03/02 04:31:51 nonaka 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 
     43 struct drm_bus_irq_cookie {
     44 	pci_intr_handle_t *intr_handles;
     45 	void *ih_cookie;
     46 };
     47 
     48 static int	drm_pci_get_irq(struct drm_device *);
     49 static int	drm_pci_irq_install(struct drm_device *,
     50 		    irqreturn_t (*)(void *), int, const char *, void *,
     51 		    struct drm_bus_irq_cookie **);
     52 static void	drm_pci_irq_uninstall(struct drm_device *,
     53 		    struct drm_bus_irq_cookie *);
     54 static const char *
     55 		drm_pci_get_name(struct drm_device *);
     56 static int	drm_pci_set_busid(struct drm_device *, struct drm_master *);
     57 static int	drm_pci_set_unique(struct drm_device *, struct drm_master *,
     58 		    struct drm_unique *);
     59 static int	drm_pci_irq_by_busid(struct drm_device *,
     60 		    struct drm_irq_busid *);
     61 
     62 const struct drm_bus drm_pci_bus = {
     63 	.bus_type = DRIVER_BUS_PCI,
     64 	.get_irq = drm_pci_get_irq,
     65 	.irq_install = drm_pci_irq_install,
     66 	.irq_uninstall = drm_pci_irq_uninstall,
     67 	.get_name = drm_pci_get_name,
     68 	.set_busid = drm_pci_set_busid,
     69 	.set_unique = drm_pci_set_unique,
     70 	.irq_by_busid = drm_pci_irq_by_busid,
     71 };
     72 
     73 static const struct pci_attach_args *
     74 drm_pci_attach_args(struct drm_device *dev)
     75 {
     76 	return &dev->pdev->pd_pa;
     77 }
     78 
     79 int
     80 drm_pci_init(struct drm_driver *driver, struct pci_driver *pdriver __unused)
     81 {
     82 
     83 	driver->bus = &drm_pci_bus;
     84 	return 0;
     85 }
     86 
     87 void
     88 drm_pci_exit(struct drm_driver *driver __unused,
     89     struct pci_driver *pdriver __unused)
     90 {
     91 }
     92 
     93 int
     94 drm_pci_attach(device_t self, const struct pci_attach_args *pa,
     95     struct pci_dev *pdev, struct drm_driver *driver, unsigned long cookie,
     96     struct drm_device **devp)
     97 {
     98 	struct drm_device *dev;
     99 	unsigned int unit;
    100 	int ret;
    101 
    102 	/* Ensure the drm agp hooks are installed.  */
    103 	/* XXX errno NetBSD->Linux */
    104 	ret = -drmkms_pci_agp_guarantee_initialized();
    105 	if (ret)
    106 		goto fail0;
    107 
    108 	/* Initialize the Linux PCI device descriptor.  */
    109 	linux_pci_dev_init(pdev, self, pa, 0);
    110 
    111 	/* Create a DRM device.  */
    112 	dev = drm_dev_alloc(driver, self);
    113 	if (dev == NULL) {
    114 		ret = -ENOMEM;
    115 		goto fail0;
    116 	}
    117 
    118 	dev->pdev = pdev;
    119 	pdev->pd_drm_dev = dev;	/* XXX Nouveau kludge.  */
    120 
    121 	/* XXX Set the power state to D0?  */
    122 
    123 	/* Set up the bus space and bus DMA tags.  */
    124 	dev->bst = pa->pa_memt;
    125 	/* XXX Let the driver say something about 32-bit vs 64-bit DMA?  */
    126 	dev->bus_dmat = (pci_dma64_available(pa)? pa->pa_dmat64 : pa->pa_dmat);
    127 	dev->dmat = dev->bus_dmat;
    128 	dev->dmat_subregion_p = false;
    129 
    130 	/* Find all the memory maps.  */
    131 	CTASSERT(PCI_NUM_RESOURCES < (SIZE_MAX / sizeof(dev->bus_maps[0])));
    132 	dev->bus_maps = kmem_zalloc(PCI_NUM_RESOURCES *
    133 	    sizeof(dev->bus_maps[0]), KM_SLEEP);
    134 	dev->bus_nmaps = PCI_NUM_RESOURCES;
    135 	for (unit = 0; unit < PCI_NUM_RESOURCES; unit++) {
    136 		struct drm_bus_map *const bm = &dev->bus_maps[unit];
    137 		const int reg = PCI_BAR(unit);
    138 		const pcireg_t type =
    139 		    pci_mapreg_type(pa->pa_pc, pa->pa_tag, reg);
    140 
    141 		/* Reject non-memory mappings.  */
    142 		if ((type & PCI_MAPREG_TYPE_MEM) != PCI_MAPREG_TYPE_MEM) {
    143 			aprint_debug_dev(self, "map %u has non-memory type:"
    144 			    " 0x%"PRIxMAX"\n", unit, (uintmax_t)type);
    145 			continue;
    146 		}
    147 
    148 		/* Inquire about it.  We'll map it in drm_core_ioremap.  */
    149 		if (pci_mapreg_info(pa->pa_pc, pa->pa_tag, reg, type,
    150 			&bm->bm_base, &bm->bm_size, &bm->bm_flags) != 0) {
    151 			aprint_debug_dev(self, "map %u failed\n", unit);
    152 			continue;
    153 		}
    154 
    155 		/* Assume since it is a memory mapping it can be linear.  */
    156 		bm->bm_flags |= BUS_SPACE_MAP_LINEAR;
    157 	}
    158 
    159 	/* Set up AGP stuff if requested.  */
    160 	if (drm_core_check_feature(dev, DRIVER_USE_AGP)) {
    161 		if (drm_pci_device_is_agp(dev))
    162 			dev->agp = drm_agp_init(dev);
    163 		if (dev->agp)
    164 			dev->agp->agp_mtrr = arch_phys_wc_add(dev->agp->base,
    165 				dev->agp->agp_info.aki_info.ai_aperture_size);
    166 	}
    167 
    168 	/* Register the DRM device and do driver-specific initialization.  */
    169 	ret = drm_dev_register(dev, cookie);
    170 	if (ret)
    171 		goto fail1;
    172 
    173 	/* Success!  */
    174 	*devp = dev;
    175 	return 0;
    176 
    177 fail2: __unused
    178 	drm_dev_unregister(dev);
    179 fail1:	drm_pci_agp_destroy(dev);
    180 	dev->bus_nmaps = 0;
    181 	kmem_free(dev->bus_maps, PCI_NUM_RESOURCES * sizeof(dev->bus_maps[0]));
    182 	if (dev->dmat_subregion_p)
    183 		bus_dmatag_destroy(dev->dmat);
    184 	drm_dev_unref(dev);
    185 fail0:	return ret;
    186 }
    187 
    188 int
    189 drm_pci_detach(struct drm_device *dev, int flags __unused)
    190 {
    191 
    192 	/* Do driver-specific detachment and unregister the device.  */
    193 	drm_dev_unregister(dev);
    194 
    195 	/* Tear down AGP stuff if necessary.  */
    196 	drm_pci_agp_destroy(dev);
    197 
    198 	/* Free the record of available bus space mappings.  */
    199 	dev->bus_nmaps = 0;
    200 	kmem_free(dev->bus_maps, PCI_NUM_RESOURCES * sizeof(dev->bus_maps[0]));
    201 
    202 	/* Tear down bus space and bus DMA tags.  */
    203 	if (dev->dmat_subregion_p)
    204 		bus_dmatag_destroy(dev->dmat);
    205 
    206 	drm_dev_unref(dev);
    207 
    208 	return 0;
    209 }
    210 
    211 void
    212 drm_pci_agp_destroy(struct drm_device *dev)
    213 {
    214 
    215 	if (dev->agp) {
    216 		arch_phys_wc_del(dev->agp->agp_mtrr);
    217 		drm_agp_clear(dev);
    218 		kfree(dev->agp); /* XXX Should go in drm_agp_clear...  */
    219 		dev->agp = NULL;
    220 	}
    221 }
    222 
    223 static int
    224 drm_pci_get_irq(struct drm_device *dev)
    225 {
    226 
    227 	/*
    228 	 * Caller expects a nonzero int, and doesn't really use it for
    229 	 * anything, so no need to pci_intr_map here.
    230 	 */
    231 	return dev->pdev->pd_pa.pa_intrpin;
    232 }
    233 
    234 static int
    235 drm_pci_irq_install(struct drm_device *dev, irqreturn_t (*handler)(void *),
    236     int flags, const char *name, void *arg, struct drm_bus_irq_cookie **cookiep)
    237 {
    238 	const struct pci_attach_args *const pa = drm_pci_attach_args(dev);
    239 	const char *intrstr;
    240 	char intrbuf[PCI_INTRSTR_LEN];
    241 	struct drm_bus_irq_cookie *irq_cookie;
    242 
    243 	irq_cookie = kmem_alloc(sizeof(*irq_cookie), KM_SLEEP);
    244 	if (irq_cookie == NULL)
    245 		return -ENOMEM;
    246 
    247 	if (dev->pdev->msi_enabled) {
    248 		irq_cookie->intr_handles = dev->pdev->intr_handles;
    249 		dev->pdev->intr_handles = NULL;
    250 	} else {
    251 		if (pci_intx_alloc(pa, &irq_cookie->intr_handles))
    252 			return -ENOENT;
    253 	}
    254 
    255 	intrstr = pci_intr_string(pa->pa_pc, irq_cookie->intr_handles[0],
    256 	    intrbuf, sizeof(intrbuf));
    257 	irq_cookie->ih_cookie = pci_intr_establish_xname(pa->pa_pc,
    258 	    irq_cookie->intr_handles[0], IPL_DRM, handler, arg, name);
    259 	if (irq_cookie->ih_cookie == NULL) {
    260 		aprint_error_dev(dev->dev,
    261 		    "couldn't establish interrupt at %s (%s)\n", intrstr, name);
    262 		return -ENOENT;
    263 	}
    264 
    265 	aprint_normal_dev(dev->dev, "interrupting at %s (%s)\n", intrstr, name);
    266 	*cookiep = irq_cookie;
    267 	return 0;
    268 }
    269 
    270 static void
    271 drm_pci_irq_uninstall(struct drm_device *dev, struct drm_bus_irq_cookie *cookie)
    272 {
    273 	const struct pci_attach_args *pa = drm_pci_attach_args(dev);
    274 
    275 	pci_intr_disestablish(pa->pa_pc, cookie->ih_cookie);
    276 	pci_intr_release(pa->pa_pc, cookie->intr_handles, 1);
    277 	kmem_free(cookie, sizeof(*cookie));
    278 }
    279 
    280 static const char *
    281 drm_pci_get_name(struct drm_device *dev)
    282 {
    283 	return "pci";		/* XXX PCI bus names?  */
    284 }
    285 
    286 static int
    287 drm_pci_format_unique(struct drm_device *dev, char *buf, size_t size)
    288 {
    289 	const unsigned int domain = device_unit(device_parent(dev->dev));
    290 	const unsigned int bus = dev->pdev->pd_pa.pa_bus;
    291 	const unsigned int device = dev->pdev->pd_pa.pa_device;
    292 	const unsigned int function = dev->pdev->pd_pa.pa_function;
    293 
    294 	return snprintf(buf, size, "pci:%04x:%02x:%02x.%d",
    295 	    domain, bus, device, function);
    296 }
    297 
    298 static int
    299 drm_pci_format_devname(struct drm_device *dev, const char *unique,
    300     char *buf, size_t size)
    301 {
    302 
    303 	return snprintf(buf, size, "%s@%s",
    304 	    device_xname(device_parent(dev->dev)),
    305 	    unique);
    306 }
    307 
    308 static int
    309 drm_pci_set_busid(struct drm_device *dev, struct drm_master *master)
    310 {
    311 	int n;
    312 	char *buf;
    313 
    314 	n = drm_pci_format_unique(dev, NULL, 0);
    315 	if (n < 0)
    316 		return -ENOSPC;	/* XXX */
    317 	if (0xff < n)
    318 		n = 0xff;
    319 
    320 	buf = kzalloc(n + 1, GFP_KERNEL);
    321 	(void)drm_pci_format_unique(dev, buf, n + 1);
    322 
    323 	if (master->unique)
    324 		kfree(master->unique);
    325 	master->unique = buf;
    326 	master->unique_len = n;
    327 	master->unique_size = n + 1;
    328 
    329 	n = drm_pci_format_devname(dev, master->unique, NULL, 0);
    330 	if (n < 0)
    331 		return -ENOSPC;	/* XXX back out? */
    332 	if (0xff < n)
    333 		n = 0xff;
    334 
    335 	buf = kzalloc(n + 1, GFP_KERNEL);
    336 	(void)drm_pci_format_devname(dev, master->unique, buf, n + 1);
    337 
    338 	if (dev->devname)
    339 		kfree(dev->devname);
    340 	dev->devname = buf;
    341 
    342 	return 0;
    343 }
    344 
    345 static int
    346 drm_pci_set_unique(struct drm_device *dev, struct drm_master *master,
    347     struct drm_unique *unique __unused)
    348 {
    349 
    350 	/*
    351 	 * XXX This is silly.  We're supposed to reject unique names
    352 	 * that don't match the ones we would generate anyway.  For
    353 	 * expedience, we'll just generate the one we would and ignore
    354 	 * whatever userland threw at us...
    355 	 */
    356 	return drm_pci_set_busid(dev, master);
    357 }
    358 
    359 static int
    360 drm_pci_irq_by_busid(struct drm_device *dev, struct drm_irq_busid *busid)
    361 {
    362 	return -ENOSYS;		/* XXX */
    363 }
    364