xref: /freebsd/sys/dev/drm2/ttm/ttm_agp_backend.c (revision 1719886f6d08408b834d270c59ffcfd821c8f63a)
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 #include <dev/drm2/drmP.h>
34 #include <dev/drm2/ttm/ttm_module.h>
35 #include <dev/drm2/ttm/ttm_bo_driver.h>
36 #include <dev/drm2/ttm/ttm_page_alloc.h>
37 #ifdef TTM_HAS_AGP
38 #include <dev/drm2/ttm/ttm_placement.h>
39 
40 struct ttm_agp_backend {
41 	struct ttm_tt ttm;
42 	vm_offset_t offset;
43 	vm_page_t *pages;
44 	device_t bridge;
45 };
46 
47 MALLOC_DEFINE(M_TTM_AGP, "ttm_agp", "TTM AGP Backend");
48 
49 static int ttm_agp_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
50 {
51 	struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm);
52 	struct drm_mm_node *node = bo_mem->mm_node;
53 	int ret;
54 	unsigned i;
55 
56 	for (i = 0; i < ttm->num_pages; i++) {
57 		vm_page_t page = ttm->pages[i];
58 
59 		if (!page)
60 			page = ttm->dummy_read_page;
61 
62 		agp_be->pages[i] = page;
63 	}
64 
65 	agp_be->offset = node->start * PAGE_SIZE;
66 	ret = -agp_bind_pages(agp_be->bridge, agp_be->pages,
67 			      ttm->num_pages << PAGE_SHIFT, agp_be->offset);
68 	if (ret)
69 		printf("[TTM] AGP Bind memory failed\n");
70 
71 	return ret;
72 }
73 
74 static int ttm_agp_unbind(struct ttm_tt *ttm)
75 {
76 	struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm);
77 
78 	return -agp_unbind_pages(agp_be->bridge, ttm->num_pages << PAGE_SHIFT,
79 				 agp_be->offset);
80 }
81 
82 static void ttm_agp_destroy(struct ttm_tt *ttm)
83 {
84 	struct ttm_agp_backend *agp_be = container_of(ttm, struct ttm_agp_backend, ttm);
85 
86 	ttm_tt_fini(ttm);
87 	free(agp_be->pages, M_TTM_AGP);
88 	free(agp_be, M_TTM_AGP);
89 }
90 
91 static struct ttm_backend_func ttm_agp_func = {
92 	.bind = ttm_agp_bind,
93 	.unbind = ttm_agp_unbind,
94 	.destroy = ttm_agp_destroy,
95 };
96 
97 struct ttm_tt *ttm_agp_tt_create(struct ttm_bo_device *bdev,
98 				 device_t bridge,
99 				 unsigned long size, uint32_t page_flags,
100 				 vm_page_t dummy_read_page)
101 {
102 	struct ttm_agp_backend *agp_be;
103 
104 	agp_be = malloc(sizeof(*agp_be), M_TTM_AGP, M_WAITOK | M_ZERO);
105 
106 	agp_be->bridge = bridge;
107 	agp_be->ttm.func = &ttm_agp_func;
108 
109 	if (ttm_tt_init(&agp_be->ttm, bdev, size, page_flags, dummy_read_page)) {
110 		free(agp_be, M_TTM_AGP);
111 		return NULL;
112 	}
113 
114 	agp_be->offset = 0;
115 	agp_be->pages = malloc(agp_be->ttm.num_pages * sizeof(*agp_be->pages),
116 			       M_TTM_AGP, M_WAITOK);
117 
118 	return &agp_be->ttm;
119 }
120 
121 int ttm_agp_tt_populate(struct ttm_tt *ttm)
122 {
123 	if (ttm->state != tt_unpopulated)
124 		return 0;
125 
126 	return ttm_pool_populate(ttm);
127 }
128 
129 void ttm_agp_tt_unpopulate(struct ttm_tt *ttm)
130 {
131 	ttm_pool_unpopulate(ttm);
132 }
133 
134 #endif
135