xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/ttm/ttm_agp_backend.c (revision 41ec02673d281bbb3d38e6c78504ce6e30c228c1)
1 /*	$NetBSD: ttm_agp_backend.c,v 1.3 2021/12/18 23:45:44 riastradh Exp $	*/
2 
3 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
4 /**************************************************************************
5  *
6  * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
7  * All Rights Reserved.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sub license, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  *
17  * The above copyright notice and this permission notice (including the
18  * next paragraph) shall be included in all copies or substantial portions
19  * of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
24  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
25  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
26  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
27  * USE OR OTHER DEALINGS IN THE SOFTWARE.
28  *
29  **************************************************************************/
30 /*
31  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
32  *          Keith Packard.
33  */
34 
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: ttm_agp_backend.c,v 1.3 2021/12/18 23:45:44 riastradh Exp $");
37 
38 #define pr_fmt(fmt) "[TTM] " fmt
39 
40 #include <drm/ttm/ttm_module.h>
41 #include <drm/ttm/ttm_bo_driver.h>
42 #include <drm/ttm/ttm_page_alloc.h>
43 #include <drm/ttm/ttm_placement.h>
44 #include <linux/agp_backend.h>
45 #include <linux/module.h>
46 #include <linux/slab.h>
47 #include <linux/io.h>
48 #include <asm/agp.h>
49 
50 struct ttm_agp_backend {
51 	struct ttm_tt ttm;
52 	struct agp_memory *mem;
53 	struct agp_bridge_data *bridge;
54 };
55 
ttm_agp_bind(struct ttm_tt * ttm,struct ttm_mem_reg * bo_mem)56 static int ttm_agp_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
57 {
58 	struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm);
59 	struct page *dummy_read_page = ttm_bo_glob.dummy_read_page;
60 	struct drm_mm_node *node = bo_mem->mm_node;
61 	struct agp_memory *mem;
62 	int ret, cached = (bo_mem->placement & TTM_PL_FLAG_CACHED);
63 	unsigned i;
64 
65 	mem = agp_allocate_memory(agp_be->bridge, ttm->num_pages, AGP_USER_MEMORY);
66 	if (unlikely(mem == NULL))
67 		return -ENOMEM;
68 
69 	mem->page_count = 0;
70 	for (i = 0; i < ttm->num_pages; i++) {
71 		struct page *page = ttm->pages[i];
72 
73 		if (!page)
74 			page = dummy_read_page;
75 
76 		mem->pages[mem->page_count++] = page;
77 	}
78 	agp_be->mem = mem;
79 
80 	mem->is_flushed = 1;
81 	mem->type = (cached) ? AGP_USER_CACHED_MEMORY : AGP_USER_MEMORY;
82 
83 	ret = agp_bind_memory(mem, node->start);
84 	if (ret)
85 		pr_err("AGP Bind memory failed\n");
86 
87 	return ret;
88 }
89 
ttm_agp_unbind(struct ttm_tt * ttm)90 static int ttm_agp_unbind(struct ttm_tt *ttm)
91 {
92 	struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm);
93 
94 	if (agp_be->mem) {
95 		if (agp_be->mem->is_bound)
96 			return agp_unbind_memory(agp_be->mem);
97 		agp_free_memory(agp_be->mem);
98 		agp_be->mem = NULL;
99 	}
100 	return 0;
101 }
102 
ttm_agp_destroy(struct ttm_tt * ttm)103 static void ttm_agp_destroy(struct ttm_tt *ttm)
104 {
105 	struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm);
106 
107 	if (agp_be->mem)
108 		ttm_agp_unbind(ttm);
109 	ttm_tt_fini(ttm);
110 	kfree(agp_be);
111 }
112 
113 static struct ttm_backend_func ttm_agp_func = {
114 	.bind = ttm_agp_bind,
115 	.unbind = ttm_agp_unbind,
116 	.destroy = ttm_agp_destroy,
117 };
118 
ttm_agp_tt_create(struct ttm_buffer_object * bo,struct agp_bridge_data * bridge,uint32_t page_flags)119 struct ttm_tt *ttm_agp_tt_create(struct ttm_buffer_object *bo,
120 				 struct agp_bridge_data *bridge,
121 				 uint32_t page_flags)
122 {
123 	struct ttm_agp_backend *agp_be;
124 
125 	agp_be = kmalloc(sizeof(*agp_be), GFP_KERNEL);
126 	if (!agp_be)
127 		return NULL;
128 
129 	agp_be->mem = NULL;
130 	agp_be->bridge = bridge;
131 	agp_be->ttm.func = &ttm_agp_func;
132 
133 	if (ttm_tt_init(&agp_be->ttm, bo, page_flags)) {
134 		kfree(agp_be);
135 		return NULL;
136 	}
137 
138 	return &agp_be->ttm;
139 }
140 EXPORT_SYMBOL(ttm_agp_tt_create);
141 
ttm_agp_tt_populate(struct ttm_tt * ttm,struct ttm_operation_ctx * ctx)142 int ttm_agp_tt_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
143 {
144 	if (ttm->state != tt_unpopulated)
145 		return 0;
146 
147 	return ttm_pool_populate(ttm, ctx);
148 }
149 EXPORT_SYMBOL(ttm_agp_tt_populate);
150 
ttm_agp_tt_unpopulate(struct ttm_tt * ttm)151 void ttm_agp_tt_unpopulate(struct ttm_tt *ttm)
152 {
153 	ttm_pool_unpopulate(ttm);
154 }
155 EXPORT_SYMBOL(ttm_agp_tt_unpopulate);
156