1/* $NetBSD: drm_agp_netbsd.h,v 1.6 2015/10/17 21:27:02 jmcneill Exp $ */
2
3/*-
4 * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Taylor R. Campbell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#ifndef _DRM_DRM_AGP_NETBSD_H_
33#define _DRM_DRM_AGP_NETBSD_H_
34
35/*
36 * XXX XXX XXX BEWARE! This file is full of abstraction violations
37 * that are ruthlessly exploited for their value as happy accidents!
38 * You have been warned!
39 */
40
41#include <sys/agpio.h>
42
43#include <dev/pci/pcivar.h> /* XXX include order botch */
44#include <dev/pci/agpreg.h>
45#include <dev/pci/agpvar.h>
46
47#include <linux/kernel.h>
48#include <linux/pci.h>
49
50#define PCI_AGP_COMMAND_FW AGPCMD_FWEN
51
52#if defined(__i386__) || defined(__x86_64__)
53#if defined(_KERNEL_OPT)
54#include "agp.h"
55#else
56#define NAGP 1
57#endif
58#if NAGP > 0
59#define __OS_HAS_AGP 1
60#endif
61__CTASSERT(PAGE_SIZE == AGP_PAGE_SIZE);
62__CTASSERT(PAGE_SHIFT == AGP_PAGE_SHIFT);
63#endif
64
65struct agp_kern_info {
66 struct agp_info aki_info;
67};
68
69struct agp_bridge_data {
70 struct agp_softc abd_sc; /* XXX Abstraction violation! */
71};
72
73/*
74 * We already have a struct agp_memory, but fortunately it looks like
75 * it may accidentally work out.
76 */
77
78#if 0
79struct agp_memory {
80 void *am_cookie;
81};
82#endif
83
84static inline struct agp_bridge_data *
85agp_find_bridge(struct pci_dev *pdev __unused)
86{
87 /*
88 * XXX How do we find the agp bridge attached to this
89 * particular PCI device?
90 */
91 return container_of((struct agp_softc *)agp_find_device(0),
92 struct agp_bridge_data, abd_sc);
93}
94
95static inline struct agp_bridge_data *
96agp_backend_acquire(struct pci_dev *pdev)
97{
98 struct agp_bridge_data *const bridge = agp_find_bridge(pdev);
99
100 if (bridge == NULL)
101 return NULL;
102
103 /* XXX We lose the error code here. */
104 if (agp_acquire(&bridge->abd_sc) != 0)
105 return NULL;
106
107 return bridge;
108}
109
110static inline void
111agp_backend_release(struct agp_bridge_data *bridge)
112{
113
114 /* XXX We lose the error code here. */
115 (void)agp_release(&bridge->abd_sc);
116}
117
118/*
119 * Happily, agp_enable will accidentally DTRT as is in NetBSD in spite
120 * of the name collision, thanks to a curious `void *' argument in its
121 * declaration...
122 */
123
124#if 0
125static inline void
126agp_enable(struct agp_bridge_data *bridge)
127{
128 ...
129}
130#endif
131
132static inline struct agp_memory *
133agp_allocate_memory(struct agp_bridge_data *bridge, size_t npages,
134 uint32_t type)
135{
136 return agp_alloc_memory(&bridge->abd_sc, (npages << AGP_PAGE_SHIFT),
137 type);
138}
139
140/*
141 * Once again, a happy accident makes agp_free_memory work out.
142 */
143
144#if 0
145static inline void
146agp_free_memory(struct agp_bridge_data *bridge, struct agp_memory *mem)
147{
148 ...
149}
150#endif
151
152/*
153 * Unfortunately, Linux's agp_bind_memory doesn't require the agp
154 * device as an argument. So we'll have to kludge that up as we go.
155 */
156#if 0
157static inline void
158agp_bind_memory(struct agp_memory *mem, size_t npages)
159{
160 agp_bind_memory(???, mem, (npages << AGP_PAGE_SHIFT));
161}
162#endif
163
164static inline void
165agp_copy_info(struct agp_bridge_data *bridge, struct agp_kern_info *info)
166{
167 agp_get_info(bridge, &info->aki_info);
168}
169
170static inline int
171drm_bind_agp(struct agp_bridge_data *bridge, struct agp_memory *mem,
172 unsigned npages)
173{
174 return agp_bind_memory(&bridge->abd_sc, mem,
175 ((size_t)npages << AGP_PAGE_SHIFT));
176}
177
178static inline int
179drm_unbind_agp(struct agp_bridge_data *bridge, struct agp_memory *mem)
180{
181 return agp_unbind_memory(&bridge->abd_sc, mem);
182}
183
184static inline void
185drm_free_agp(struct agp_bridge_data *bridge, struct agp_memory *mem,
186 int npages __unused)
187{
188 agp_free_memory(&bridge->abd_sc, mem);
189}
190
191#endif /* _DRM_DRM_AGP_NETBSD_H_ */
192