xref: /freebsd/sys/dev/drm2/ttm/ttm_agp_backend.c (revision 517904de5ccac643589c71ac0d2751797f89e4f9)
1 /**************************************************************************
2  *
3  * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 /*
28  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29  *          Keith Packard.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include <dev/drm2/drmP.h>
36 #include <dev/drm2/ttm/ttm_module.h>
37 #include <dev/drm2/ttm/ttm_bo_driver.h>
38 #include <dev/drm2/ttm/ttm_page_alloc.h>
39 #ifdef TTM_HAS_AGP
40 #include <dev/drm2/ttm/ttm_placement.h>
41 
42 struct ttm_agp_backend {
43 	struct ttm_tt ttm;
44 	vm_offset_t offset;
45 	vm_page_t *pages;
46 	device_t bridge;
47 };
48 
49 MALLOC_DEFINE(M_TTM_AGP, "ttm_agp", "TTM AGP Backend");
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 drm_mm_node *node = bo_mem->mm_node;
55 	int ret;
56 	unsigned i;
57 
58 	for (i = 0; i < ttm->num_pages; i++) {
59 		vm_page_t page = ttm->pages[i];
60 
61 		if (!page)
62 			page = ttm->dummy_read_page;
63 
64 		agp_be->pages[i] = page;
65 	}
66 
67 	agp_be->offset = node->start * PAGE_SIZE;
68 	ret = -agp_bind_pages(agp_be->bridge, agp_be->pages,
69 			      ttm->num_pages << PAGE_SHIFT, agp_be->offset);
70 	if (ret)
71 		printf("[TTM] AGP Bind memory failed\n");
72 
73 	return ret;
74 }
75 
76 static int ttm_agp_unbind(struct ttm_tt *ttm)
77 {
78 	struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm);
79 
80 	return -agp_unbind_pages(agp_be->bridge, ttm->num_pages << PAGE_SHIFT,
81 				 agp_be->offset);
82 }
83 
84 static void ttm_agp_destroy(struct ttm_tt *ttm)
85 {
86 	struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm);
87 
88 	ttm_tt_fini(ttm);
89 	free(agp_be->pages, M_TTM_AGP);
90 	free(agp_be, M_TTM_AGP);
91 }
92 
93 static struct ttm_backend_func ttm_agp_func = {
94 	.bind = ttm_agp_bind,
95 	.unbind = ttm_agp_unbind,
96 	.destroy = ttm_agp_destroy,
97 };
98 
99 struct ttm_tt *ttm_agp_tt_create(struct ttm_bo_device *bdev,
100 				 device_t bridge,
101 				 unsigned long size, uint32_t page_flags,
102 				 vm_page_t dummy_read_page)
103 {
104 	struct ttm_agp_backend *agp_be;
105 
106 	agp_be = malloc(sizeof(*agp_be), M_TTM_AGP, M_WAITOK | M_ZERO);
107 
108 	agp_be->bridge = bridge;
109 	agp_be->ttm.func = &ttm_agp_func;
110 
111 	if (ttm_tt_init(&agp_be->ttm, bdev, size, page_flags, dummy_read_page)) {
112 		free(agp_be, M_TTM_AGP);
113 		return NULL;
114 	}
115 
116 	agp_be->offset = 0;
117 	agp_be->pages = malloc(agp_be->ttm.num_pages * sizeof(*agp_be->pages),
118 			       M_TTM_AGP, M_WAITOK);
119 
120 	return &agp_be->ttm;
121 }
122 
123 int ttm_agp_tt_populate(struct ttm_tt *ttm)
124 {
125 	if (ttm->state != tt_unpopulated)
126 		return 0;
127 
128 	return ttm_pool_populate(ttm);
129 }
130 
131 void ttm_agp_tt_unpopulate(struct ttm_tt *ttm)
132 {
133 	ttm_pool_unpopulate(ttm);
134 }
135 
136 #endif
137