Home | History | Annotate | Line # | Download | only in drm
drm_agp_hook.c revision 1.2
      1  1.2       tnn /*	$NetBSD: drm_agp_hook.c,v 1.2 2018/08/30 19:03:14 tnn Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*-
      4  1.1  riastrad  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5  1.1  riastrad  * All rights reserved.
      6  1.1  riastrad  *
      7  1.1  riastrad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  riastrad  * by Taylor R. Campbell.
      9  1.1  riastrad  *
     10  1.1  riastrad  * Redistribution and use in source and binary forms, with or without
     11  1.1  riastrad  * modification, are permitted provided that the following conditions
     12  1.1  riastrad  * are met:
     13  1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     14  1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     15  1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     18  1.1  riastrad  *
     19  1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  riastrad  */
     31  1.1  riastrad 
     32  1.1  riastrad #include <sys/cdefs.h>
     33  1.2       tnn __KERNEL_RCSID(0, "$NetBSD: drm_agp_hook.c,v 1.2 2018/08/30 19:03:14 tnn Exp $");
     34  1.1  riastrad 
     35  1.1  riastrad #include <sys/types.h>
     36  1.1  riastrad #include <sys/condvar.h>
     37  1.1  riastrad #include <sys/errno.h>
     38  1.1  riastrad #include <sys/mutex.h>
     39  1.1  riastrad #include <sys/once.h>
     40  1.1  riastrad 
     41  1.1  riastrad #include <drm/drmP.h>
     42  1.1  riastrad #include <drm/drm_internal.h>
     43  1.1  riastrad 
     44  1.1  riastrad static struct {
     45  1.1  riastrad 	kmutex_t			lock;
     46  1.1  riastrad 	kcondvar_t			cv;
     47  1.1  riastrad 	unsigned			refcnt; /* at most one per device */
     48  1.1  riastrad 	const struct drm_agp_hooks	*hooks;
     49  1.1  riastrad } agp_hooks __cacheline_aligned;
     50  1.1  riastrad 
     51  1.1  riastrad void
     52  1.1  riastrad drm_agp_hooks_init(void)
     53  1.1  riastrad {
     54  1.1  riastrad 
     55  1.1  riastrad 	mutex_init(&agp_hooks.lock, MUTEX_DEFAULT, IPL_NONE);
     56  1.1  riastrad 	cv_init(&agp_hooks.cv, "agphooks");
     57  1.1  riastrad 	agp_hooks.refcnt = 0;
     58  1.1  riastrad 	agp_hooks.hooks = NULL;
     59  1.1  riastrad }
     60  1.1  riastrad 
     61  1.1  riastrad void
     62  1.1  riastrad drm_agp_hooks_fini(void)
     63  1.1  riastrad {
     64  1.1  riastrad 
     65  1.1  riastrad 	KASSERT(agp_hooks.hooks == NULL);
     66  1.1  riastrad 	KASSERT(agp_hooks.refcnt == 0);
     67  1.1  riastrad 	cv_destroy(&agp_hooks.cv);
     68  1.1  riastrad 	mutex_destroy(&agp_hooks.lock);
     69  1.1  riastrad }
     70  1.1  riastrad 
     71  1.1  riastrad int
     72  1.1  riastrad drm_agp_register(const struct drm_agp_hooks *hooks)
     73  1.1  riastrad {
     74  1.1  riastrad 	int error = 0;
     75  1.1  riastrad 
     76  1.1  riastrad 	mutex_enter(&agp_hooks.lock);
     77  1.1  riastrad 	if (agp_hooks.refcnt) {
     78  1.1  riastrad 		KASSERT(agp_hooks.hooks);
     79  1.1  riastrad 		error = EBUSY;
     80  1.1  riastrad 	} else {
     81  1.1  riastrad 		agp_hooks.refcnt++;
     82  1.1  riastrad 		agp_hooks.hooks = hooks;
     83  1.1  riastrad 	}
     84  1.1  riastrad 	mutex_exit(&agp_hooks.lock);
     85  1.1  riastrad 
     86  1.1  riastrad 	return error;
     87  1.1  riastrad }
     88  1.1  riastrad 
     89  1.1  riastrad int
     90  1.1  riastrad drm_agp_deregister(const struct drm_agp_hooks *hooks)
     91  1.1  riastrad {
     92  1.2       tnn 	int error = 0;
     93  1.1  riastrad 
     94  1.1  riastrad 	mutex_enter(&agp_hooks.lock);
     95  1.1  riastrad 	KASSERT(agp_hooks.hooks == hooks);
     96  1.1  riastrad 	if (agp_hooks.refcnt > 1) {
     97  1.1  riastrad 		error = EBUSY;
     98  1.1  riastrad 	} else {
     99  1.1  riastrad 		agp_hooks.refcnt = 0;
    100  1.1  riastrad 		agp_hooks.hooks = NULL;
    101  1.1  riastrad 	}
    102  1.1  riastrad 	mutex_exit(&agp_hooks.lock);
    103  1.1  riastrad 
    104  1.1  riastrad 	return error;
    105  1.1  riastrad }
    106  1.1  riastrad 
    107  1.1  riastrad static const struct drm_agp_hooks *
    108  1.1  riastrad drm_agp_hooks_acquire(void)
    109  1.1  riastrad {
    110  1.1  riastrad 	const struct drm_agp_hooks *hooks;
    111  1.1  riastrad 
    112  1.1  riastrad 	mutex_enter(&agp_hooks.lock);
    113  1.1  riastrad 	if (agp_hooks.refcnt == 0) {
    114  1.1  riastrad 		hooks = NULL;
    115  1.1  riastrad 	} else {
    116  1.1  riastrad 		KASSERT(agp_hooks.refcnt < UINT_MAX);
    117  1.1  riastrad 		agp_hooks.refcnt++;
    118  1.1  riastrad 		hooks = agp_hooks.hooks;
    119  1.1  riastrad 	}
    120  1.1  riastrad 	mutex_exit(&agp_hooks.lock);
    121  1.1  riastrad 
    122  1.1  riastrad 	return hooks;
    123  1.1  riastrad }
    124  1.1  riastrad 
    125  1.1  riastrad static void
    126  1.1  riastrad drm_agp_hooks_release(const struct drm_agp_hooks *hooks)
    127  1.1  riastrad {
    128  1.1  riastrad 
    129  1.1  riastrad 	mutex_enter(&agp_hooks.lock);
    130  1.1  riastrad 	KASSERT(agp_hooks.hooks == hooks);
    131  1.1  riastrad 	KASSERT(agp_hooks.refcnt);
    132  1.1  riastrad 	if (--agp_hooks.refcnt == 0)
    133  1.1  riastrad 		cv_broadcast(&agp_hooks.cv);
    134  1.1  riastrad 	mutex_exit(&agp_hooks.lock);
    135  1.1  riastrad }
    136  1.1  riastrad 
    137  1.1  riastrad struct drm_agp_head *
    138  1.1  riastrad drm_agp_init(struct drm_device *dev)
    139  1.1  riastrad {
    140  1.1  riastrad 	const struct drm_agp_hooks *hooks;
    141  1.1  riastrad 	struct drm_agp_head *agp;
    142  1.1  riastrad 
    143  1.1  riastrad 	if ((hooks = drm_agp_hooks_acquire()) == NULL)
    144  1.1  riastrad 		return NULL;
    145  1.1  riastrad 	agp = hooks->agph_init(dev);
    146  1.1  riastrad 	if (agp == NULL)
    147  1.1  riastrad 		drm_agp_hooks_release(hooks);
    148  1.1  riastrad 
    149  1.1  riastrad 	return agp;
    150  1.1  riastrad }
    151  1.1  riastrad 
    152  1.1  riastrad void
    153  1.1  riastrad drm_agp_fini(struct drm_device *dev)
    154  1.1  riastrad {
    155  1.1  riastrad 
    156  1.1  riastrad 	if (dev->agp == NULL)
    157  1.1  riastrad 		return;
    158  1.1  riastrad 	dev->agp->hooks->agph_clear(dev);
    159  1.1  riastrad 	drm_agp_hooks_release(dev->agp->hooks);
    160  1.1  riastrad 	kfree(dev->agp);
    161  1.1  riastrad 	dev->agp = NULL;
    162  1.1  riastrad }
    163  1.1  riastrad 
    164  1.1  riastrad void
    165  1.1  riastrad drm_agp_clear(struct drm_device *dev)
    166  1.1  riastrad {
    167  1.1  riastrad 
    168  1.1  riastrad 	if (dev->agp == NULL)
    169  1.1  riastrad 		return;
    170  1.1  riastrad 	dev->agp->hooks->agph_clear(dev);
    171  1.1  riastrad }
    172  1.1  riastrad 
    173  1.1  riastrad int
    174  1.1  riastrad drm_agp_acquire(struct drm_device *dev)
    175  1.1  riastrad {
    176  1.1  riastrad 
    177  1.1  riastrad 	if (dev->agp == NULL)
    178  1.1  riastrad 		return -ENODEV;
    179  1.1  riastrad 	return dev->agp->hooks->agph_acquire(dev);
    180  1.1  riastrad }
    181  1.1  riastrad 
    182  1.1  riastrad int
    183  1.1  riastrad drm_agp_release(struct drm_device *dev)
    184  1.1  riastrad {
    185  1.1  riastrad 
    186  1.1  riastrad 	if (dev->agp == NULL)
    187  1.1  riastrad 		return -EINVAL;
    188  1.1  riastrad 	return dev->agp->hooks->agph_release(dev);
    189  1.1  riastrad }
    190  1.1  riastrad 
    191  1.1  riastrad int
    192  1.1  riastrad drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode)
    193  1.1  riastrad {
    194  1.1  riastrad 
    195  1.1  riastrad 	if (dev->agp == NULL)
    196  1.1  riastrad 		return -EINVAL;
    197  1.1  riastrad 	return dev->agp->hooks->agph_enable(dev, mode);
    198  1.1  riastrad }
    199  1.1  riastrad 
    200  1.1  riastrad int
    201  1.1  riastrad drm_agp_info(struct drm_device *dev, struct drm_agp_info *info)
    202  1.1  riastrad {
    203  1.1  riastrad 
    204  1.1  riastrad 	if (dev->agp == NULL)
    205  1.1  riastrad 		return -EINVAL;
    206  1.1  riastrad 	return dev->agp->hooks->agph_info(dev, info);
    207  1.1  riastrad }
    208  1.1  riastrad 
    209  1.1  riastrad int
    210  1.1  riastrad drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
    211  1.1  riastrad {
    212  1.1  riastrad 
    213  1.1  riastrad 	if (dev->agp == NULL)
    214  1.1  riastrad 		return -EINVAL;
    215  1.1  riastrad 	return dev->agp->hooks->agph_alloc(dev, request);
    216  1.1  riastrad }
    217  1.1  riastrad 
    218  1.1  riastrad int
    219  1.1  riastrad drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
    220  1.1  riastrad {
    221  1.1  riastrad 
    222  1.1  riastrad 	if (dev->agp == NULL)
    223  1.1  riastrad 		return -EINVAL;
    224  1.1  riastrad 	return dev->agp->hooks->agph_free(dev, request);
    225  1.1  riastrad }
    226  1.1  riastrad 
    227  1.1  riastrad int
    228  1.1  riastrad drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
    229  1.1  riastrad {
    230  1.1  riastrad 
    231  1.1  riastrad 	if (dev->agp == NULL)
    232  1.1  riastrad 		return -EINVAL;
    233  1.1  riastrad 	return dev->agp->hooks->agph_bind(dev, request);
    234  1.1  riastrad }
    235  1.1  riastrad 
    236  1.1  riastrad int
    237  1.1  riastrad drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
    238  1.1  riastrad {
    239  1.1  riastrad 
    240  1.1  riastrad 	if (dev->agp == NULL)
    241  1.1  riastrad 		return -EINVAL;
    242  1.1  riastrad 	return dev->agp->hooks->agph_unbind(dev, request);
    243  1.1  riastrad }
    244  1.1  riastrad 
    245  1.1  riastrad #define	DEFINE_AGP_HOOK_IOCTL(NAME, FIELD)				      \
    246  1.1  riastrad int									      \
    247  1.1  riastrad NAME(struct drm_device *dev, void *data, struct drm_file *file)		      \
    248  1.1  riastrad {									      \
    249  1.1  riastrad 									      \
    250  1.1  riastrad 	if (dev->agp == NULL)						      \
    251  1.1  riastrad 		return -ENODEV;						      \
    252  1.1  riastrad 	return dev->agp->hooks->FIELD(dev, data, file);			      \
    253  1.1  riastrad }
    254  1.1  riastrad 
    255  1.1  riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_acquire_ioctl, agph_acquire_ioctl)
    256  1.1  riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_release_ioctl, agph_release_ioctl)
    257  1.1  riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_enable_ioctl, agph_enable_ioctl)
    258  1.1  riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_info_ioctl, agph_info_ioctl)
    259  1.1  riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_alloc_ioctl, agph_alloc_ioctl)
    260  1.1  riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_free_ioctl, agph_free_ioctl)
    261  1.1  riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_bind_ioctl, agph_bind_ioctl)
    262  1.1  riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_unbind_ioctl, agph_unbind_ioctl)
    263  1.1  riastrad 
    264  1.1  riastrad void __pci_iomem *
    265  1.1  riastrad drm_agp_borrow(struct drm_device *dev, unsigned bar, bus_size_t size)
    266  1.1  riastrad {
    267  1.1  riastrad 	const struct drm_agp_hooks *hooks;
    268  1.1  riastrad 	void __pci_iomem *iomem;
    269  1.1  riastrad 
    270  1.1  riastrad 	if ((hooks = drm_agp_hooks_acquire()) == NULL)
    271  1.1  riastrad 		return NULL;
    272  1.1  riastrad 	iomem = hooks->agph_borrow(dev, bar, size);
    273  1.1  riastrad 	drm_agp_hooks_release(hooks);
    274  1.1  riastrad 
    275  1.1  riastrad 	return iomem;
    276  1.1  riastrad }
    277  1.1  riastrad 
    278  1.1  riastrad void
    279  1.1  riastrad drm_agp_flush(void)
    280  1.1  riastrad {
    281  1.1  riastrad 	const struct drm_agp_hooks *hooks;
    282  1.1  riastrad 
    283  1.1  riastrad 	if ((hooks = drm_agp_hooks_acquire()) == NULL)
    284  1.1  riastrad 		return;
    285  1.1  riastrad 	hooks->agph_flush();
    286  1.1  riastrad 	drm_agp_hooks_release(hooks);
    287  1.1  riastrad }
    288