Home | History | Annotate | Line # | Download | only in drm
      1  1.7  riastrad /*	$NetBSD: drm_agp_hook.c,v 1.7 2022/07/19 22:24:34 riastradh 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.7  riastrad __KERNEL_RCSID(0, "$NetBSD: drm_agp_hook.c,v 1.7 2022/07/19 22:24:34 riastradh 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.6  riastrad #include <drm/drm_agpsupport.h>
     42  1.6  riastrad #include <drm/drm_drv.h>
     43  1.4  riastrad #include "../dist/drm/drm_internal.h"
     44  1.1  riastrad 
     45  1.1  riastrad static struct {
     46  1.1  riastrad 	kmutex_t			lock;
     47  1.1  riastrad 	kcondvar_t			cv;
     48  1.1  riastrad 	unsigned			refcnt; /* at most one per device */
     49  1.1  riastrad 	const struct drm_agp_hooks	*hooks;
     50  1.1  riastrad } agp_hooks __cacheline_aligned;
     51  1.1  riastrad 
     52  1.1  riastrad void
     53  1.1  riastrad drm_agp_hooks_init(void)
     54  1.1  riastrad {
     55  1.1  riastrad 
     56  1.1  riastrad 	mutex_init(&agp_hooks.lock, MUTEX_DEFAULT, IPL_NONE);
     57  1.1  riastrad 	cv_init(&agp_hooks.cv, "agphooks");
     58  1.1  riastrad 	agp_hooks.refcnt = 0;
     59  1.1  riastrad 	agp_hooks.hooks = NULL;
     60  1.1  riastrad }
     61  1.1  riastrad 
     62  1.1  riastrad void
     63  1.1  riastrad drm_agp_hooks_fini(void)
     64  1.1  riastrad {
     65  1.1  riastrad 
     66  1.1  riastrad 	KASSERT(agp_hooks.hooks == NULL);
     67  1.1  riastrad 	KASSERT(agp_hooks.refcnt == 0);
     68  1.1  riastrad 	cv_destroy(&agp_hooks.cv);
     69  1.1  riastrad 	mutex_destroy(&agp_hooks.lock);
     70  1.1  riastrad }
     71  1.1  riastrad 
     72  1.1  riastrad int
     73  1.1  riastrad drm_agp_register(const struct drm_agp_hooks *hooks)
     74  1.1  riastrad {
     75  1.1  riastrad 	int error = 0;
     76  1.1  riastrad 
     77  1.1  riastrad 	mutex_enter(&agp_hooks.lock);
     78  1.1  riastrad 	if (agp_hooks.refcnt) {
     79  1.1  riastrad 		KASSERT(agp_hooks.hooks);
     80  1.1  riastrad 		error = EBUSY;
     81  1.1  riastrad 	} else {
     82  1.1  riastrad 		agp_hooks.refcnt++;
     83  1.1  riastrad 		agp_hooks.hooks = hooks;
     84  1.1  riastrad 	}
     85  1.1  riastrad 	mutex_exit(&agp_hooks.lock);
     86  1.1  riastrad 
     87  1.1  riastrad 	return error;
     88  1.1  riastrad }
     89  1.1  riastrad 
     90  1.1  riastrad int
     91  1.1  riastrad drm_agp_deregister(const struct drm_agp_hooks *hooks)
     92  1.1  riastrad {
     93  1.2       tnn 	int error = 0;
     94  1.1  riastrad 
     95  1.1  riastrad 	mutex_enter(&agp_hooks.lock);
     96  1.1  riastrad 	KASSERT(agp_hooks.hooks == hooks);
     97  1.1  riastrad 	if (agp_hooks.refcnt > 1) {
     98  1.1  riastrad 		error = EBUSY;
     99  1.1  riastrad 	} else {
    100  1.1  riastrad 		agp_hooks.refcnt = 0;
    101  1.1  riastrad 		agp_hooks.hooks = NULL;
    102  1.1  riastrad 	}
    103  1.1  riastrad 	mutex_exit(&agp_hooks.lock);
    104  1.1  riastrad 
    105  1.1  riastrad 	return error;
    106  1.1  riastrad }
    107  1.1  riastrad 
    108  1.1  riastrad static const struct drm_agp_hooks *
    109  1.1  riastrad drm_agp_hooks_acquire(void)
    110  1.1  riastrad {
    111  1.1  riastrad 	const struct drm_agp_hooks *hooks;
    112  1.1  riastrad 
    113  1.1  riastrad 	mutex_enter(&agp_hooks.lock);
    114  1.1  riastrad 	if (agp_hooks.refcnt == 0) {
    115  1.1  riastrad 		hooks = NULL;
    116  1.1  riastrad 	} else {
    117  1.1  riastrad 		KASSERT(agp_hooks.refcnt < UINT_MAX);
    118  1.1  riastrad 		agp_hooks.refcnt++;
    119  1.1  riastrad 		hooks = agp_hooks.hooks;
    120  1.1  riastrad 	}
    121  1.1  riastrad 	mutex_exit(&agp_hooks.lock);
    122  1.1  riastrad 
    123  1.1  riastrad 	return hooks;
    124  1.1  riastrad }
    125  1.1  riastrad 
    126  1.1  riastrad static void
    127  1.1  riastrad drm_agp_hooks_release(const struct drm_agp_hooks *hooks)
    128  1.1  riastrad {
    129  1.1  riastrad 
    130  1.1  riastrad 	mutex_enter(&agp_hooks.lock);
    131  1.1  riastrad 	KASSERT(agp_hooks.hooks == hooks);
    132  1.1  riastrad 	KASSERT(agp_hooks.refcnt);
    133  1.1  riastrad 	if (--agp_hooks.refcnt == 0)
    134  1.1  riastrad 		cv_broadcast(&agp_hooks.cv);
    135  1.1  riastrad 	mutex_exit(&agp_hooks.lock);
    136  1.1  riastrad }
    137  1.1  riastrad 
    138  1.1  riastrad struct drm_agp_head *
    139  1.1  riastrad drm_agp_init(struct drm_device *dev)
    140  1.1  riastrad {
    141  1.1  riastrad 	const struct drm_agp_hooks *hooks;
    142  1.1  riastrad 	struct drm_agp_head *agp;
    143  1.1  riastrad 
    144  1.1  riastrad 	if ((hooks = drm_agp_hooks_acquire()) == NULL)
    145  1.1  riastrad 		return NULL;
    146  1.1  riastrad 	agp = hooks->agph_init(dev);
    147  1.1  riastrad 	if (agp == NULL)
    148  1.1  riastrad 		drm_agp_hooks_release(hooks);
    149  1.3       mrg 	else
    150  1.3       mrg 		agp->hooks = hooks;
    151  1.1  riastrad 
    152  1.1  riastrad 	return agp;
    153  1.1  riastrad }
    154  1.1  riastrad 
    155  1.1  riastrad void
    156  1.1  riastrad drm_agp_fini(struct drm_device *dev)
    157  1.1  riastrad {
    158  1.1  riastrad 
    159  1.1  riastrad 	if (dev->agp == NULL)
    160  1.1  riastrad 		return;
    161  1.1  riastrad 	dev->agp->hooks->agph_clear(dev);
    162  1.1  riastrad 	drm_agp_hooks_release(dev->agp->hooks);
    163  1.1  riastrad 	kfree(dev->agp);
    164  1.1  riastrad 	dev->agp = NULL;
    165  1.1  riastrad }
    166  1.1  riastrad 
    167  1.1  riastrad void
    168  1.5  riastrad drm_legacy_agp_clear(struct drm_device *dev)
    169  1.1  riastrad {
    170  1.1  riastrad 
    171  1.1  riastrad 	if (dev->agp == NULL)
    172  1.1  riastrad 		return;
    173  1.1  riastrad 	dev->agp->hooks->agph_clear(dev);
    174  1.1  riastrad }
    175  1.1  riastrad 
    176  1.1  riastrad int
    177  1.1  riastrad drm_agp_acquire(struct drm_device *dev)
    178  1.1  riastrad {
    179  1.1  riastrad 
    180  1.1  riastrad 	if (dev->agp == NULL)
    181  1.1  riastrad 		return -ENODEV;
    182  1.1  riastrad 	return dev->agp->hooks->agph_acquire(dev);
    183  1.1  riastrad }
    184  1.1  riastrad 
    185  1.1  riastrad int
    186  1.1  riastrad drm_agp_release(struct drm_device *dev)
    187  1.1  riastrad {
    188  1.1  riastrad 
    189  1.1  riastrad 	if (dev->agp == NULL)
    190  1.1  riastrad 		return -EINVAL;
    191  1.1  riastrad 	return dev->agp->hooks->agph_release(dev);
    192  1.1  riastrad }
    193  1.1  riastrad 
    194  1.1  riastrad int
    195  1.1  riastrad drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode)
    196  1.1  riastrad {
    197  1.1  riastrad 
    198  1.1  riastrad 	if (dev->agp == NULL)
    199  1.1  riastrad 		return -EINVAL;
    200  1.1  riastrad 	return dev->agp->hooks->agph_enable(dev, mode);
    201  1.1  riastrad }
    202  1.1  riastrad 
    203  1.1  riastrad int
    204  1.1  riastrad drm_agp_info(struct drm_device *dev, struct drm_agp_info *info)
    205  1.1  riastrad {
    206  1.1  riastrad 
    207  1.1  riastrad 	if (dev->agp == NULL)
    208  1.1  riastrad 		return -EINVAL;
    209  1.1  riastrad 	return dev->agp->hooks->agph_info(dev, info);
    210  1.1  riastrad }
    211  1.1  riastrad 
    212  1.1  riastrad int
    213  1.1  riastrad drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
    214  1.1  riastrad {
    215  1.1  riastrad 
    216  1.1  riastrad 	if (dev->agp == NULL)
    217  1.1  riastrad 		return -EINVAL;
    218  1.1  riastrad 	return dev->agp->hooks->agph_alloc(dev, request);
    219  1.1  riastrad }
    220  1.1  riastrad 
    221  1.1  riastrad int
    222  1.1  riastrad drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
    223  1.1  riastrad {
    224  1.1  riastrad 
    225  1.1  riastrad 	if (dev->agp == NULL)
    226  1.1  riastrad 		return -EINVAL;
    227  1.1  riastrad 	return dev->agp->hooks->agph_free(dev, request);
    228  1.1  riastrad }
    229  1.1  riastrad 
    230  1.1  riastrad int
    231  1.1  riastrad drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
    232  1.1  riastrad {
    233  1.1  riastrad 
    234  1.1  riastrad 	if (dev->agp == NULL)
    235  1.1  riastrad 		return -EINVAL;
    236  1.1  riastrad 	return dev->agp->hooks->agph_bind(dev, request);
    237  1.1  riastrad }
    238  1.1  riastrad 
    239  1.1  riastrad int
    240  1.1  riastrad drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
    241  1.1  riastrad {
    242  1.1  riastrad 
    243  1.1  riastrad 	if (dev->agp == NULL)
    244  1.1  riastrad 		return -EINVAL;
    245  1.1  riastrad 	return dev->agp->hooks->agph_unbind(dev, request);
    246  1.1  riastrad }
    247  1.1  riastrad 
    248  1.1  riastrad #define	DEFINE_AGP_HOOK_IOCTL(NAME, FIELD)				      \
    249  1.1  riastrad int									      \
    250  1.1  riastrad NAME(struct drm_device *dev, void *data, struct drm_file *file)		      \
    251  1.1  riastrad {									      \
    252  1.1  riastrad 									      \
    253  1.1  riastrad 	if (dev->agp == NULL)						      \
    254  1.1  riastrad 		return -ENODEV;						      \
    255  1.1  riastrad 	return dev->agp->hooks->FIELD(dev, data, file);			      \
    256  1.1  riastrad }
    257  1.1  riastrad 
    258  1.1  riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_acquire_ioctl, agph_acquire_ioctl)
    259  1.1  riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_release_ioctl, agph_release_ioctl)
    260  1.1  riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_enable_ioctl, agph_enable_ioctl)
    261  1.1  riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_info_ioctl, agph_info_ioctl)
    262  1.1  riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_alloc_ioctl, agph_alloc_ioctl)
    263  1.1  riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_free_ioctl, agph_free_ioctl)
    264  1.1  riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_bind_ioctl, agph_bind_ioctl)
    265  1.1  riastrad DEFINE_AGP_HOOK_IOCTL(drm_agp_unbind_ioctl, agph_unbind_ioctl)
    266  1.1  riastrad 
    267  1.1  riastrad void
    268  1.1  riastrad drm_agp_flush(void)
    269  1.1  riastrad {
    270  1.1  riastrad 	const struct drm_agp_hooks *hooks;
    271  1.1  riastrad 
    272  1.1  riastrad 	if ((hooks = drm_agp_hooks_acquire()) == NULL)
    273  1.1  riastrad 		return;
    274  1.1  riastrad 	hooks->agph_flush();
    275  1.1  riastrad 	drm_agp_hooks_release(hooks);
    276  1.1  riastrad }
    277