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