1/* $OpenBSD: drm_agpsupport.c,v 1.29 2022/01/14 06:52: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_device.h>
43#include <drm/drm_drv.h>
44#include <drm/drm_file.h>
45#include <drm/drm_print.h>
46
47#include "drm_legacy.h"
48
49#if IS_ENABLED(CONFIG_AGP)
50
51int
52drm_legacy_agp_info(struct drm_device * dev, struct drm_agp_info *info)
53{
54	struct agp_info	*kern;
55
56	if (dev->agp == NULL || !dev->agp->acquired)
57		return (EINVAL);
58
59	kern = &dev->agp->info;
60	agp_get_info(dev->agp->agpdev, kern);
61	info->agp_version_major = 1;
62	info->agp_version_minor = 0;
63	info->mode = kern->ai_mode;
64	info->aperture_base = kern->ai_aperture_base;
65	info->aperture_size = kern->ai_aperture_size;
66	info->memory_allowed = kern->ai_memory_allowed;
67	info->memory_used = kern->ai_memory_used;
68	info->id_vendor = kern->ai_devid & 0xffff;
69	info->id_device = kern->ai_devid >> 16;
70
71	return (0);
72}
73
74int
75drm_legacy_agp_acquire(struct drm_device *dev)
76{
77	int	retcode;
78
79	if (dev->agp == NULL || dev->agp->acquired)
80		return (EINVAL);
81
82	retcode = agp_acquire(dev->agp->agpdev);
83	if (retcode)
84		return (retcode);
85
86	dev->agp->acquired = 1;
87
88	return (0);
89}
90
91int
92drm_legacy_agp_release(struct drm_device * dev)
93{
94	if (dev->agp == NULL || !dev->agp->acquired)
95		return (EINVAL);
96	agp_release(dev->agp->agpdev);
97	dev->agp->acquired = 0;
98
99	return (0);
100}
101
102int
103drm_legacy_agp_enable(struct drm_device *dev, drm_agp_mode_t mode)
104{
105	int	retcode = 0;
106
107	if (dev->agp == NULL || !dev->agp->acquired)
108		return (EINVAL);
109
110	dev->agp->mode = mode.mode;
111	if ((retcode = agp_enable(dev->agp->agpdev, mode.mode)) == 0)
112		dev->agp->enabled = 1;
113	return (retcode);
114}
115
116void
117drm_legacy_agp_takedown(struct drm_device *dev)
118{
119	if (dev->agp == NULL)
120		return;
121
122	drm_legacy_agp_release(dev);
123	dev->agp->enabled  = 0;
124}
125
126struct drm_agp_head *
127drm_legacy_agp_init(struct drm_device *dev)
128{
129	struct agp_softc	*agpdev;
130	struct drm_agp_head	*head = NULL;
131	int		 	 agp_available = 1;
132
133	agpdev = agp_find_device(0);
134	if (agpdev == NULL)
135		agp_available = 0;
136
137	DRM_DEBUG("agp_available = %d\n", agp_available);
138
139	if (agp_available) {
140		head = mallocarray(1, sizeof(*head), M_DRM, M_NOWAIT | M_ZERO);
141		if (head == NULL)
142			return (NULL);
143		head->agpdev = agpdev;
144		agp_get_info(agpdev, &head->info);
145		head->base = head->info.ai_aperture_base;
146		head->cant_use_aperture = (head->base == 0);
147		TAILQ_INIT(&head->memory);
148	}
149	return (head);
150}
151
152#endif /* IS_ENABLED(CONFIG_AGP) */
153