1 /**************************************************************************
3 * Copyright © 2009-2011 VMware, Inc., Palo Alto, CA., USA
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "vmwgfx_drv.h"
30 #include "ttm/ttm_bo_driver.h"
32 #define VMW_PPN_SIZE sizeof(unsigned long)
34 static int vmw_gmr2_bind(struct vmw_private
*dev_priv
,
36 unsigned long num_pages
,
39 SVGAFifoCmdDefineGMR2 define_cmd
;
40 SVGAFifoCmdRemapGMR2 remap_cmd
;
41 uint32_t define_size
= sizeof(define_cmd
) + 4;
42 uint32_t remap_size
= VMW_PPN_SIZE
* num_pages
+ sizeof(remap_cmd
) + 4;
47 cmd_orig
= cmd
= vmw_fifo_reserve(dev_priv
, define_size
+ remap_size
);
48 if (unlikely(cmd
== NULL
))
51 define_cmd
.gmrId
= gmr_id
;
52 define_cmd
.numPages
= num_pages
;
54 remap_cmd
.gmrId
= gmr_id
;
55 remap_cmd
.flags
= (VMW_PPN_SIZE
> sizeof(*cmd
)) ?
56 SVGA_REMAP_GMR2_PPN64
: SVGA_REMAP_GMR2_PPN32
;
57 remap_cmd
.offsetPages
= 0;
58 remap_cmd
.numPages
= num_pages
;
60 *cmd
++ = SVGA_CMD_DEFINE_GMR2
;
61 memcpy(cmd
, &define_cmd
, sizeof(define_cmd
));
62 cmd
+= sizeof(define_cmd
) / sizeof(uint32
);
64 *cmd
++ = SVGA_CMD_REMAP_GMR2
;
65 memcpy(cmd
, &remap_cmd
, sizeof(remap_cmd
));
66 cmd
+= sizeof(remap_cmd
) / sizeof(uint32
);
68 for (i
= 0; i
< num_pages
; ++i
) {
70 *cmd
= page_to_pfn(*pages
++);
72 *((uint64_t *)cmd
) = page_to_pfn(*pages
++);
74 cmd
+= VMW_PPN_SIZE
/ sizeof(*cmd
);
77 vmw_fifo_commit(dev_priv
, define_size
+ remap_size
);
82 static void vmw_gmr2_unbind(struct vmw_private
*dev_priv
,
85 SVGAFifoCmdDefineGMR2 define_cmd
;
86 uint32_t define_size
= sizeof(define_cmd
) + 4;
89 cmd
= vmw_fifo_reserve(dev_priv
, define_size
);
90 if (unlikely(cmd
== NULL
)) {
91 DRM_ERROR("GMR2 unbind failed.\n");
94 define_cmd
.gmrId
= gmr_id
;
95 define_cmd
.numPages
= 0;
97 *cmd
++ = SVGA_CMD_DEFINE_GMR2
;
98 memcpy(cmd
, &define_cmd
, sizeof(define_cmd
));
100 vmw_fifo_commit(dev_priv
, define_size
);
104 * FIXME: Adjust to the ttm lowmem / highmem storage to minimize
105 * the number of used descriptors.
108 static int vmw_gmr_build_descriptors(struct list_head
*desc_pages
,
109 struct page
*pages
[],
110 unsigned long num_pages
)
112 struct page
*page
, *next
;
113 struct svga_guest_mem_descriptor
*page_virtual
= NULL
;
114 struct svga_guest_mem_descriptor
*desc_virtual
= NULL
;
115 unsigned int desc_per_page
;
116 unsigned long prev_pfn
;
120 desc_per_page
= PAGE_SIZE
/
121 sizeof(struct svga_guest_mem_descriptor
) - 1;
123 while (likely(num_pages
!= 0)) {
124 page
= alloc_page(__GFP_HIGHMEM
);
125 if (unlikely(page
== NULL
)) {
130 list_add_tail(&page
->lru
, desc_pages
);
133 * Point previous page terminating descriptor to this
134 * page before unmapping it.
137 if (likely(page_virtual
!= NULL
)) {
138 desc_virtual
->ppn
= page_to_pfn(page
);
139 kunmap_atomic(page_virtual
, KM_USER0
);
142 page_virtual
= kmap_atomic(page
, KM_USER0
);
143 desc_virtual
= page_virtual
- 1;
146 while (likely(num_pages
!= 0)) {
147 pfn
= page_to_pfn(*pages
);
149 if (pfn
!= prev_pfn
+ 1) {
151 if (desc_virtual
- page_virtual
==
155 (++desc_virtual
)->ppn
= cpu_to_le32(pfn
);
156 desc_virtual
->num_pages
= cpu_to_le32(1);
159 le32_to_cpu(desc_virtual
->num_pages
);
160 desc_virtual
->num_pages
= cpu_to_le32(tmp
+ 1);
167 (++desc_virtual
)->ppn
= cpu_to_le32(0);
168 desc_virtual
->num_pages
= cpu_to_le32(0);
171 if (likely(page_virtual
!= NULL
))
172 kunmap_atomic(page_virtual
, KM_USER0
);
176 list_for_each_entry_safe(page
, next
, desc_pages
, lru
) {
177 list_del_init(&page
->lru
);
183 static inline void vmw_gmr_free_descriptors(struct list_head
*desc_pages
)
185 struct page
*page
, *next
;
187 list_for_each_entry_safe(page
, next
, desc_pages
, lru
) {
188 list_del_init(&page
->lru
);
193 static void vmw_gmr_fire_descriptors(struct vmw_private
*dev_priv
,
194 int gmr_id
, struct list_head
*desc_pages
)
198 if (unlikely(list_empty(desc_pages
)))
201 page
= list_entry(desc_pages
->next
, struct page
, lru
);
203 mutex_lock(&dev_priv
->hw_mutex
);
205 vmw_write(dev_priv
, SVGA_REG_GMR_ID
, gmr_id
);
207 vmw_write(dev_priv
, SVGA_REG_GMR_DESCRIPTOR
, page_to_pfn(page
));
210 mutex_unlock(&dev_priv
->hw_mutex
);
215 * FIXME: Adjust to the ttm lowmem / highmem storage to minimize
216 * the number of used descriptors.
219 static unsigned long vmw_gmr_count_descriptors(struct page
*pages
[],
220 unsigned long num_pages
)
222 unsigned long prev_pfn
= ~(0UL);
224 unsigned long descriptors
= 0;
226 while (num_pages
--) {
227 pfn
= page_to_pfn(*pages
++);
228 if (prev_pfn
+ 1 != pfn
)
236 int vmw_gmr_bind(struct vmw_private
*dev_priv
,
237 struct page
*pages
[],
238 unsigned long num_pages
,
241 struct list_head desc_pages
;
244 if (likely(dev_priv
->capabilities
& SVGA_CAP_GMR2
))
245 return vmw_gmr2_bind(dev_priv
, pages
, num_pages
, gmr_id
);
247 if (unlikely(!(dev_priv
->capabilities
& SVGA_CAP_GMR
)))
250 if (vmw_gmr_count_descriptors(pages
, num_pages
) >
251 dev_priv
->max_gmr_descriptors
)
254 INIT_LIST_HEAD(&desc_pages
);
256 ret
= vmw_gmr_build_descriptors(&desc_pages
, pages
, num_pages
);
257 if (unlikely(ret
!= 0))
260 vmw_gmr_fire_descriptors(dev_priv
, gmr_id
, &desc_pages
);
261 vmw_gmr_free_descriptors(&desc_pages
);
267 void vmw_gmr_unbind(struct vmw_private
*dev_priv
, int gmr_id
)
269 if (likely(dev_priv
->capabilities
& SVGA_CAP_GMR2
)) {
270 vmw_gmr2_unbind(dev_priv
, gmr_id
);
274 mutex_lock(&dev_priv
->hw_mutex
);
275 vmw_write(dev_priv
, SVGA_REG_GMR_ID
, gmr_id
);
277 vmw_write(dev_priv
, SVGA_REG_GMR_DESCRIPTOR
, 0);
279 mutex_unlock(&dev_priv
->hw_mutex
);