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