1 /* $OpenBSD: drm_agpsupport.c,v 1.28 2020/06/08 04:47:58 jsg Exp $ */ 2 /*- 3 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas. 4 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. 5 * All Rights Reserved. 6 * 7 * Permission is hereby granted, free of charge, to any person obtaining a 8 * copy of this software and associated documentation files (the "Software"), 9 * to deal in the Software without restriction, including without limitation 10 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 11 * and/or sell copies of the Software, and to permit persons to whom the 12 * Software is furnished to do so, subject to the following conditions: 13 * 14 * The above copyright notice and this permission notice (including the next 15 * paragraph) shall be included in all copies or substantial portions of the 16 * Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 * OTHER DEALINGS IN THE SOFTWARE. 25 * 26 * Author: 27 * Rickard E. (Rik) Faith <faith@valinux.com> 28 * Gareth Hughes <gareth@valinux.com> 29 * 30 */ 31 32 /* 33 * Support code for tying the kernel AGP support to DRM drivers. 34 */ 35 36 #include <linux/module.h> 37 #include <linux/pci.h> 38 #include <linux/slab.h> 39 40 #include <asm/agp.h> 41 42 #include <drm/drm_agpsupport.h> 43 #include <drm/drm_device.h> 44 #include <drm/drm_drv.h> 45 #include <drm/drm_file.h> 46 #include <drm/drm_print.h> 47 48 #include "drm_legacy.h" 49 50 #if IS_ENABLED(CONFIG_AGP) 51 52 int 53 drm_agp_info(struct drm_device * dev, struct drm_agp_info *info) 54 { 55 struct agp_info *kern; 56 57 if (dev->agp == NULL || !dev->agp->acquired) 58 return (EINVAL); 59 60 kern = &dev->agp->info; 61 agp_get_info(dev->agp->agpdev, kern); 62 info->agp_version_major = 1; 63 info->agp_version_minor = 0; 64 info->mode = kern->ai_mode; 65 info->aperture_base = kern->ai_aperture_base; 66 info->aperture_size = kern->ai_aperture_size; 67 info->memory_allowed = kern->ai_memory_allowed; 68 info->memory_used = kern->ai_memory_used; 69 info->id_vendor = kern->ai_devid & 0xffff; 70 info->id_device = kern->ai_devid >> 16; 71 72 return (0); 73 } 74 75 int 76 drm_agp_acquire(struct drm_device *dev) 77 { 78 int retcode; 79 80 if (dev->agp == NULL || dev->agp->acquired) 81 return (EINVAL); 82 83 retcode = agp_acquire(dev->agp->agpdev); 84 if (retcode) 85 return (retcode); 86 87 dev->agp->acquired = 1; 88 89 return (0); 90 } 91 92 int 93 drm_agp_release(struct drm_device * dev) 94 { 95 if (dev->agp == NULL || !dev->agp->acquired) 96 return (EINVAL); 97 agp_release(dev->agp->agpdev); 98 dev->agp->acquired = 0; 99 100 return (0); 101 } 102 103 int 104 drm_agp_enable(struct drm_device *dev, drm_agp_mode_t mode) 105 { 106 int retcode = 0; 107 108 if (dev->agp == NULL || !dev->agp->acquired) 109 return (EINVAL); 110 111 dev->agp->mode = mode.mode; 112 if ((retcode = agp_enable(dev->agp->agpdev, mode.mode)) == 0) 113 dev->agp->enabled = 1; 114 return (retcode); 115 } 116 117 void 118 drm_agp_takedown(struct drm_device *dev) 119 { 120 if (dev->agp == NULL) 121 return; 122 123 drm_agp_release(dev); 124 dev->agp->enabled = 0; 125 } 126 127 struct drm_agp_head * 128 drm_agp_init(void) 129 { 130 struct agp_softc *agpdev; 131 struct drm_agp_head *head = NULL; 132 int agp_available = 1; 133 134 agpdev = agp_find_device(0); 135 if (agpdev == NULL) 136 agp_available = 0; 137 138 DRM_DEBUG("agp_available = %d\n", agp_available); 139 140 if (agp_available) { 141 head = mallocarray(1, sizeof(*head), M_DRM, M_NOWAIT | M_ZERO); 142 if (head == NULL) 143 return (NULL); 144 head->agpdev = agpdev; 145 agp_get_info(agpdev, &head->info); 146 head->base = head->info.ai_aperture_base; 147 head->cant_use_aperture = (head->base == 0); 148 TAILQ_INIT(&head->memory); 149 } 150 return (head); 151 } 152 153 #endif /* IS_ENABLED(CONFIG_AGP) */ 154