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