Home | History | Annotate | Line # | Download | only in pci
drm_pci.c revision 1.1.2.5
      1 /*	$NetBSD: drm_pci.c,v 1.1.2.5 2013/07/24 03:54:14 riastradh 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.1.2.5 2013/07/24 03:54:14 riastradh 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 static int	drm_pci_get_irq(struct drm_device *);
     44 static int	drm_pci_irq_install(struct drm_device *,
     45 		    irqreturn_t (*)(void *), int, const char *, void *,
     46 		    struct drm_bus_irq_cookie **);
     47 static void	drm_pci_irq_uninstall(struct drm_device *,
     48 		    struct drm_bus_irq_cookie *);
     49 static const char *
     50 		drm_pci_get_name(struct drm_device *);
     51 static int	drm_pci_set_busid(struct drm_device *, struct drm_master *);
     52 static int	drm_pci_set_unique(struct drm_device *, struct drm_master *,
     53 		    struct drm_unique *);
     54 static int	drm_pci_irq_by_busid(struct drm_device *,
     55 		    struct drm_irq_busid *);
     56 static int	drm_pci_agp_init(struct drm_device *);
     57 
     58 const struct drm_bus drm_pci_bus = {
     59 	.bus_type = DRIVER_BUS_PCI,
     60 	.get_irq = drm_pci_get_irq,
     61 	.irq_install = drm_pci_irq_install,
     62 	.irq_uninstall = drm_pci_irq_uninstall,
     63 	.get_name = drm_pci_get_name,
     64 	.set_busid = drm_pci_set_busid,
     65 	.set_unique = drm_pci_set_unique,
     66 	.irq_by_busid = drm_pci_irq_by_busid,
     67 	.agp_init = drm_pci_agp_init,
     68 };
     69 
     70 static const struct pci_attach_args *
     71 drm_pci_attach_args(struct drm_device *dev)
     72 {
     73 	return &dev->pdev->pd_pa;
     74 }
     75 
     76 void
     77 drm_pci_attach(device_t self, const struct pci_attach_args *pa,
     78     struct pci_dev *pdev, struct drm_device *dev)
     79 {
     80 
     81 	dev->driver->bus = &drm_pci_bus;
     82 
     83 	linux_pci_dev_init(pdev, self, pa, 0);
     84 
     85 	dev->pdev = pdev;
     86 	dev->pci_vendor = pdev->vendor;
     87 	dev->pci_device = pdev->device;
     88 
     89 	/* XXX Set the power state to D0?  */
     90 }
     91 
     92 int
     93 drm_pci_detach(struct drm_device *dev __unused, int flags __unused)
     94 {
     95 
     96 	/* XXX Disestablish irqs or anything?  */
     97 	return 0;
     98 }
     99 
    100 static int
    101 drm_pci_get_irq(struct drm_device *dev)
    102 {
    103 	pci_intr_handle_t ih_pih;
    104 	int ih_int;
    105 
    106 	/*
    107 	 * This is a compile-time assertion that the types match.  If
    108 	 * this fails, we have to change a bunch of drm code that uses
    109 	 * int for intr handles.
    110 	 */
    111 	KASSERT(&ih_pih != &ih_int);
    112 
    113 	if (pci_intr_map(drm_pci_attach_args(dev), &ih_pih))
    114 		return -1;	/* XXX Hope -1 is an invalid intr handle.  */
    115 
    116 	ih_int = ih_pih;
    117 	return ih_int;
    118 }
    119 
    120 static int
    121 drm_pci_irq_install(struct drm_device *dev, irqreturn_t (*handler)(void *),
    122     int flags, const char *name, void *arg,
    123     struct drm_bus_irq_cookie **cookiep)
    124 {
    125 	const struct pci_attach_args *const pa = drm_pci_attach_args(dev);
    126 	pci_intr_handle_t ih;
    127 	const char *intrstr;
    128 	void *ih_cookie;
    129 
    130 	if (pci_intr_map(pa, &ih))
    131 		return -ENOENT;
    132 
    133 	intrstr = pci_intr_string(pa->pa_pc, ih);
    134 	ih_cookie = pci_intr_establish(pa->pa_pc, ih, IPL_DRM, handler, arg);
    135 	if (ih_cookie == NULL) {
    136 		aprint_error_dev(dev->dev,
    137 		    "couldn't establish interrupt at %s (%s)\n",
    138 		    intrstr, name);
    139 		return -ENOENT;
    140 	}
    141 
    142 	aprint_normal_dev(dev->dev, "interrupting at %s (%s)\n",
    143 	    intrstr, name);
    144 	*cookiep = (struct drm_bus_irq_cookie *)ih_cookie;
    145 	return 0;
    146 }
    147 
    148 static void
    149 drm_pci_irq_uninstall(struct drm_device *dev,
    150     struct drm_bus_irq_cookie *cookie)
    151 {
    152 	const struct pci_attach_args *pa = drm_pci_attach_args(dev);
    153 
    154 	pci_intr_disestablish(pa->pa_pc, (void *)cookie);
    155 }
    156 
    157 static const char *
    158 drm_pci_get_name(struct drm_device *dev)
    159 {
    160 	return "pci";		/* XXX PCI bus names?  */
    161 }
    162 
    163 static int
    164 drm_pci_set_busid(struct drm_device *dev, struct drm_master *master)
    165 {
    166 	return -ENOSYS;		/* XXX PCI bus ids?  */
    167 }
    168 
    169 static int
    170 drm_pci_set_unique(struct drm_device *dev, struct drm_master *master,
    171     struct drm_unique *unique)
    172 {
    173 	return -ENOSYS;		/* XXX */
    174 }
    175 
    176 static int
    177 drm_pci_irq_by_busid(struct drm_device *dev, struct drm_irq_busid *busid)
    178 {
    179 	return -ENOSYS;		/* XXX */
    180 }
    181 
    182 static int
    183 drm_pci_agp_init(struct drm_device *dev)
    184 {
    185 	return 0;		/* XXX */
    186 }
    187