Commit | Line | Data |
---|---|---|
fb1d9738 JB |
1 | /************************************************************************** |
2 | * | |
3 | * Copyright © 2007 David Airlie | |
4 | * Copyright © 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 | ||
2d1a8a48 PG |
29 | #include <linux/export.h> |
30 | ||
760285e7 | 31 | #include <drm/drmP.h> |
fb1d9738 | 32 | #include "vmwgfx_drv.h" |
a278724a | 33 | #include "vmwgfx_kms.h" |
fb1d9738 | 34 | |
760285e7 | 35 | #include <drm/ttm/ttm_placement.h> |
fb1d9738 JB |
36 | |
37 | #define VMW_DIRTY_DELAY (HZ / 30) | |
38 | ||
39 | struct vmw_fb_par { | |
40 | struct vmw_private *vmw_priv; | |
41 | ||
42 | void *vmalloc; | |
43 | ||
a278724a | 44 | struct mutex bo_mutex; |
fb1d9738 JB |
45 | struct vmw_dma_buffer *vmw_bo; |
46 | struct ttm_bo_kmap_obj map; | |
a278724a TH |
47 | void *bo_ptr; |
48 | unsigned bo_size; | |
49 | struct drm_framebuffer *set_fb; | |
50 | struct drm_display_mode *set_mode; | |
51 | u32 fb_x; | |
52 | u32 fb_y; | |
53 | bool bo_iowrite; | |
fb1d9738 JB |
54 | |
55 | u32 pseudo_palette[17]; | |
56 | ||
fb1d9738 JB |
57 | unsigned max_width; |
58 | unsigned max_height; | |
59 | ||
fb1d9738 JB |
60 | struct { |
61 | spinlock_t lock; | |
62 | bool active; | |
63 | unsigned x1; | |
64 | unsigned y1; | |
65 | unsigned x2; | |
66 | unsigned y2; | |
67 | } dirty; | |
a278724a TH |
68 | |
69 | struct drm_crtc *crtc; | |
70 | struct drm_connector *con; | |
71 | ||
72 | bool local_mode; | |
fb1d9738 JB |
73 | }; |
74 | ||
75 | static int vmw_fb_setcolreg(unsigned regno, unsigned red, unsigned green, | |
76 | unsigned blue, unsigned transp, | |
77 | struct fb_info *info) | |
78 | { | |
79 | struct vmw_fb_par *par = info->par; | |
80 | u32 *pal = par->pseudo_palette; | |
81 | ||
82 | if (regno > 15) { | |
83 | DRM_ERROR("Bad regno %u.\n", regno); | |
84 | return 1; | |
85 | } | |
86 | ||
a278724a | 87 | switch (par->set_fb->depth) { |
fb1d9738 JB |
88 | case 24: |
89 | case 32: | |
90 | pal[regno] = ((red & 0xff00) << 8) | | |
91 | (green & 0xff00) | | |
92 | ((blue & 0xff00) >> 8); | |
93 | break; | |
94 | default: | |
a278724a TH |
95 | DRM_ERROR("Bad depth %u, bpp %u.\n", par->set_fb->depth, |
96 | par->set_fb->bits_per_pixel); | |
fb1d9738 JB |
97 | return 1; |
98 | } | |
99 | ||
100 | return 0; | |
101 | } | |
102 | ||
103 | static int vmw_fb_check_var(struct fb_var_screeninfo *var, | |
104 | struct fb_info *info) | |
105 | { | |
106 | int depth = var->bits_per_pixel; | |
107 | struct vmw_fb_par *par = info->par; | |
108 | struct vmw_private *vmw_priv = par->vmw_priv; | |
109 | ||
110 | switch (var->bits_per_pixel) { | |
111 | case 32: | |
112 | depth = (var->transp.length > 0) ? 32 : 24; | |
113 | break; | |
114 | default: | |
115 | DRM_ERROR("Bad bpp %u.\n", var->bits_per_pixel); | |
116 | return -EINVAL; | |
117 | } | |
118 | ||
119 | switch (depth) { | |
120 | case 24: | |
121 | var->red.offset = 16; | |
122 | var->green.offset = 8; | |
123 | var->blue.offset = 0; | |
124 | var->red.length = 8; | |
125 | var->green.length = 8; | |
126 | var->blue.length = 8; | |
127 | var->transp.length = 0; | |
128 | var->transp.offset = 0; | |
129 | break; | |
130 | case 32: | |
131 | var->red.offset = 16; | |
132 | var->green.offset = 8; | |
133 | var->blue.offset = 0; | |
134 | var->red.length = 8; | |
135 | var->green.length = 8; | |
136 | var->blue.length = 8; | |
137 | var->transp.length = 8; | |
138 | var->transp.offset = 24; | |
139 | break; | |
140 | default: | |
141 | DRM_ERROR("Bad depth %u.\n", depth); | |
142 | return -EINVAL; | |
143 | } | |
144 | ||
d7e1958d JB |
145 | if ((var->xoffset + var->xres) > par->max_width || |
146 | (var->yoffset + var->yres) > par->max_height) { | |
fb1d9738 JB |
147 | DRM_ERROR("Requested geom can not fit in framebuffer\n"); |
148 | return -EINVAL; | |
149 | } | |
150 | ||
e133e737 | 151 | if (!vmw_kms_validate_mode_vram(vmw_priv, |
aa6de142 | 152 | var->xres * var->bits_per_pixel/8, |
e133e737 TH |
153 | var->yoffset + var->yres)) { |
154 | DRM_ERROR("Requested geom can not fit in framebuffer\n"); | |
155 | return -EINVAL; | |
156 | } | |
157 | ||
fb1d9738 JB |
158 | return 0; |
159 | } | |
160 | ||
fb1d9738 JB |
161 | static int vmw_fb_blank(int blank, struct fb_info *info) |
162 | { | |
163 | return 0; | |
164 | } | |
165 | ||
166 | /* | |
167 | * Dirty code | |
168 | */ | |
169 | ||
170 | static void vmw_fb_dirty_flush(struct vmw_fb_par *par) | |
171 | { | |
172 | struct vmw_private *vmw_priv = par->vmw_priv; | |
173 | struct fb_info *info = vmw_priv->fb_info; | |
a278724a TH |
174 | unsigned long irq_flags; |
175 | s32 dst_x1, dst_x2, dst_y1, dst_y2, w, h; | |
176 | u32 cpp, max_x, max_y; | |
177 | struct drm_clip_rect clip; | |
178 | struct drm_framebuffer *cur_fb; | |
179 | u8 *src_ptr, *dst_ptr; | |
fb1d9738 | 180 | |
09e2601b TH |
181 | if (vmw_priv->suspended) |
182 | return; | |
183 | ||
a278724a TH |
184 | mutex_lock(&par->bo_mutex); |
185 | cur_fb = par->set_fb; | |
186 | if (!cur_fb) | |
187 | goto out_unlock; | |
fb1d9738 | 188 | |
a278724a TH |
189 | spin_lock_irqsave(&par->dirty.lock, irq_flags); |
190 | if (!par->dirty.active) { | |
191 | spin_unlock_irqrestore(&par->dirty.lock, irq_flags); | |
192 | goto out_unlock; | |
fb1d9738 JB |
193 | } |
194 | ||
a278724a TH |
195 | /* |
196 | * Handle panning when copying from vmalloc to framebuffer. | |
197 | * Clip dirty area to framebuffer. | |
198 | */ | |
199 | cpp = (cur_fb->bits_per_pixel + 7) / 8; | |
200 | max_x = par->fb_x + cur_fb->width; | |
201 | max_y = par->fb_y + cur_fb->height; | |
202 | ||
203 | dst_x1 = par->dirty.x1 - par->fb_x; | |
204 | dst_y1 = par->dirty.y1 - par->fb_y; | |
205 | dst_x1 = max_t(s32, dst_x1, 0); | |
206 | dst_y1 = max_t(s32, dst_y1, 0); | |
207 | ||
208 | dst_x2 = par->dirty.x2 - par->fb_x; | |
209 | dst_y2 = par->dirty.y2 - par->fb_y; | |
210 | dst_x2 = min_t(s32, dst_x2, max_x); | |
211 | dst_y2 = min_t(s32, dst_y2, max_y); | |
212 | w = dst_x2 - dst_x1; | |
213 | h = dst_y2 - dst_y1; | |
214 | w = max_t(s32, 0, w); | |
215 | h = max_t(s32, 0, h); | |
fb1d9738 | 216 | |
a278724a TH |
217 | par->dirty.x1 = par->dirty.x2 = 0; |
218 | par->dirty.y1 = par->dirty.y2 = 0; | |
219 | spin_unlock_irqrestore(&par->dirty.lock, irq_flags); | |
220 | ||
221 | if (w && h) { | |
222 | dst_ptr = (u8 *)par->bo_ptr + | |
223 | (dst_y1 * par->set_fb->pitches[0] + dst_x1 * cpp); | |
224 | src_ptr = (u8 *)par->vmalloc + | |
225 | ((dst_y1 + par->fb_y) * info->fix.line_length + | |
226 | (dst_x1 + par->fb_x) * cpp); | |
227 | ||
228 | while (h-- > 0) { | |
229 | memcpy(dst_ptr, src_ptr, w*cpp); | |
230 | dst_ptr += par->set_fb->pitches[0]; | |
231 | src_ptr += info->fix.line_length; | |
232 | } | |
233 | ||
234 | clip.x1 = dst_x1; | |
235 | clip.x2 = dst_x2; | |
236 | clip.y1 = dst_y1; | |
237 | clip.y2 = dst_y2; | |
238 | ||
239 | WARN_ON_ONCE(par->set_fb->funcs->dirty(cur_fb, NULL, 0, 0, | |
240 | &clip, 1)); | |
241 | vmw_fifo_flush(vmw_priv, false); | |
fb1d9738 | 242 | } |
a278724a TH |
243 | out_unlock: |
244 | mutex_unlock(&par->bo_mutex); | |
fb1d9738 JB |
245 | } |
246 | ||
247 | static void vmw_fb_dirty_mark(struct vmw_fb_par *par, | |
248 | unsigned x1, unsigned y1, | |
249 | unsigned width, unsigned height) | |
250 | { | |
251 | struct fb_info *info = par->vmw_priv->fb_info; | |
252 | unsigned long flags; | |
253 | unsigned x2 = x1 + width; | |
254 | unsigned y2 = y1 + height; | |
255 | ||
256 | spin_lock_irqsave(&par->dirty.lock, flags); | |
257 | if (par->dirty.x1 == par->dirty.x2) { | |
258 | par->dirty.x1 = x1; | |
259 | par->dirty.y1 = y1; | |
260 | par->dirty.x2 = x2; | |
261 | par->dirty.y2 = y2; | |
262 | /* if we are active start the dirty work | |
263 | * we share the work with the defio system */ | |
264 | if (par->dirty.active) | |
265 | schedule_delayed_work(&info->deferred_work, VMW_DIRTY_DELAY); | |
266 | } else { | |
267 | if (x1 < par->dirty.x1) | |
268 | par->dirty.x1 = x1; | |
269 | if (y1 < par->dirty.y1) | |
270 | par->dirty.y1 = y1; | |
271 | if (x2 > par->dirty.x2) | |
272 | par->dirty.x2 = x2; | |
273 | if (y2 > par->dirty.y2) | |
274 | par->dirty.y2 = y2; | |
275 | } | |
276 | spin_unlock_irqrestore(&par->dirty.lock, flags); | |
277 | } | |
278 | ||
a278724a TH |
279 | static int vmw_fb_pan_display(struct fb_var_screeninfo *var, |
280 | struct fb_info *info) | |
281 | { | |
282 | struct vmw_fb_par *par = info->par; | |
283 | ||
284 | if ((var->xoffset + var->xres) > var->xres_virtual || | |
285 | (var->yoffset + var->yres) > var->yres_virtual) { | |
286 | DRM_ERROR("Requested panning can not fit in framebuffer\n"); | |
287 | return -EINVAL; | |
288 | } | |
289 | ||
290 | mutex_lock(&par->bo_mutex); | |
291 | par->fb_x = var->xoffset; | |
292 | par->fb_y = var->yoffset; | |
293 | if (par->set_fb) | |
294 | vmw_fb_dirty_mark(par, par->fb_x, par->fb_y, par->set_fb->width, | |
295 | par->set_fb->height); | |
296 | mutex_unlock(&par->bo_mutex); | |
297 | ||
298 | return 0; | |
299 | } | |
300 | ||
fb1d9738 JB |
301 | static void vmw_deferred_io(struct fb_info *info, |
302 | struct list_head *pagelist) | |
303 | { | |
304 | struct vmw_fb_par *par = info->par; | |
305 | unsigned long start, end, min, max; | |
306 | unsigned long flags; | |
307 | struct page *page; | |
308 | int y1, y2; | |
309 | ||
310 | min = ULONG_MAX; | |
311 | max = 0; | |
312 | list_for_each_entry(page, pagelist, lru) { | |
313 | start = page->index << PAGE_SHIFT; | |
314 | end = start + PAGE_SIZE - 1; | |
315 | min = min(min, start); | |
316 | max = max(max, end); | |
317 | } | |
318 | ||
319 | if (min < max) { | |
320 | y1 = min / info->fix.line_length; | |
321 | y2 = (max / info->fix.line_length) + 1; | |
322 | ||
323 | spin_lock_irqsave(&par->dirty.lock, flags); | |
324 | par->dirty.x1 = 0; | |
325 | par->dirty.y1 = y1; | |
326 | par->dirty.x2 = info->var.xres; | |
327 | par->dirty.y2 = y2; | |
328 | spin_unlock_irqrestore(&par->dirty.lock, flags); | |
329 | } | |
330 | ||
331 | vmw_fb_dirty_flush(par); | |
332 | }; | |
333 | ||
b9eb1a61 | 334 | static struct fb_deferred_io vmw_defio = { |
fb1d9738 JB |
335 | .delay = VMW_DIRTY_DELAY, |
336 | .deferred_io = vmw_deferred_io, | |
337 | }; | |
338 | ||
339 | /* | |
340 | * Draw code | |
341 | */ | |
342 | ||
343 | static void vmw_fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect) | |
344 | { | |
345 | cfb_fillrect(info, rect); | |
346 | vmw_fb_dirty_mark(info->par, rect->dx, rect->dy, | |
347 | rect->width, rect->height); | |
348 | } | |
349 | ||
350 | static void vmw_fb_copyarea(struct fb_info *info, const struct fb_copyarea *region) | |
351 | { | |
352 | cfb_copyarea(info, region); | |
353 | vmw_fb_dirty_mark(info->par, region->dx, region->dy, | |
354 | region->width, region->height); | |
355 | } | |
356 | ||
357 | static void vmw_fb_imageblit(struct fb_info *info, const struct fb_image *image) | |
358 | { | |
359 | cfb_imageblit(info, image); | |
360 | vmw_fb_dirty_mark(info->par, image->dx, image->dy, | |
361 | image->width, image->height); | |
362 | } | |
363 | ||
364 | /* | |
365 | * Bring up code | |
366 | */ | |
367 | ||
fb1d9738 JB |
368 | static int vmw_fb_create_bo(struct vmw_private *vmw_priv, |
369 | size_t size, struct vmw_dma_buffer **out) | |
370 | { | |
371 | struct vmw_dma_buffer *vmw_bo; | |
fb1d9738 JB |
372 | int ret; |
373 | ||
294adf7d | 374 | (void) ttm_write_lock(&vmw_priv->reservation_sem, false); |
fb1d9738 JB |
375 | |
376 | vmw_bo = kmalloc(sizeof(*vmw_bo), GFP_KERNEL); | |
294adf7d TH |
377 | if (!vmw_bo) { |
378 | ret = -ENOMEM; | |
fb1d9738 | 379 | goto err_unlock; |
294adf7d | 380 | } |
fb1d9738 JB |
381 | |
382 | ret = vmw_dmabuf_init(vmw_priv, vmw_bo, size, | |
a278724a | 383 | &vmw_sys_placement, |
fb1d9738 JB |
384 | false, |
385 | &vmw_dmabuf_bo_free); | |
386 | if (unlikely(ret != 0)) | |
387 | goto err_unlock; /* init frees the buffer on failure */ | |
388 | ||
389 | *out = vmw_bo; | |
12617971 | 390 | ttm_write_unlock(&vmw_priv->reservation_sem); |
fb1d9738 JB |
391 | |
392 | return 0; | |
393 | ||
394 | err_unlock: | |
12617971 | 395 | ttm_write_unlock(&vmw_priv->reservation_sem); |
fb1d9738 JB |
396 | return ret; |
397 | } | |
398 | ||
a278724a TH |
399 | static int vmw_fb_compute_depth(struct fb_var_screeninfo *var, |
400 | int *depth) | |
401 | { | |
402 | switch (var->bits_per_pixel) { | |
403 | case 32: | |
404 | *depth = (var->transp.length > 0) ? 32 : 24; | |
405 | break; | |
406 | default: | |
407 | DRM_ERROR("Bad bpp %u.\n", var->bits_per_pixel); | |
408 | return -EINVAL; | |
409 | } | |
410 | ||
411 | return 0; | |
412 | } | |
413 | ||
414 | static int vmw_fb_kms_detach(struct vmw_fb_par *par, | |
415 | bool detach_bo, | |
416 | bool unref_bo) | |
417 | { | |
418 | struct drm_framebuffer *cur_fb = par->set_fb; | |
419 | int ret; | |
420 | ||
421 | /* Detach the KMS framebuffer from crtcs */ | |
422 | if (par->set_mode) { | |
423 | struct drm_mode_set set; | |
424 | ||
425 | set.crtc = par->crtc; | |
426 | set.x = 0; | |
427 | set.y = 0; | |
428 | set.mode = NULL; | |
429 | set.fb = NULL; | |
430 | set.num_connectors = 1; | |
431 | set.connectors = &par->con; | |
432 | ret = drm_mode_set_config_internal(&set); | |
433 | if (ret) { | |
434 | DRM_ERROR("Could not unset a mode.\n"); | |
435 | return ret; | |
436 | } | |
437 | drm_mode_destroy(par->vmw_priv->dev, par->set_mode); | |
438 | par->set_mode = NULL; | |
439 | } | |
440 | ||
441 | if (cur_fb) { | |
442 | drm_framebuffer_unreference(cur_fb); | |
443 | par->set_fb = NULL; | |
444 | } | |
445 | ||
446 | if (par->vmw_bo && detach_bo) { | |
447 | if (par->bo_ptr) { | |
448 | ttm_bo_kunmap(&par->map); | |
449 | par->bo_ptr = NULL; | |
450 | } | |
451 | if (unref_bo) | |
452 | vmw_dmabuf_unreference(&par->vmw_bo); | |
453 | else | |
454 | vmw_dmabuf_unpin(par->vmw_priv, par->vmw_bo, false); | |
455 | } | |
456 | ||
457 | return 0; | |
458 | } | |
459 | ||
460 | static int vmw_fb_kms_framebuffer(struct fb_info *info) | |
461 | { | |
462 | struct drm_mode_fb_cmd mode_cmd; | |
463 | struct vmw_fb_par *par = info->par; | |
464 | struct fb_var_screeninfo *var = &info->var; | |
465 | struct drm_framebuffer *cur_fb; | |
466 | struct vmw_framebuffer *vfb; | |
467 | int ret = 0; | |
468 | size_t new_bo_size; | |
469 | ||
470 | ret = vmw_fb_compute_depth(var, &mode_cmd.depth); | |
471 | if (ret) | |
472 | return ret; | |
473 | ||
474 | mode_cmd.width = var->xres; | |
475 | mode_cmd.height = var->yres; | |
476 | mode_cmd.bpp = var->bits_per_pixel; | |
477 | mode_cmd.pitch = ((mode_cmd.bpp + 7) / 8) * mode_cmd.width; | |
478 | ||
479 | cur_fb = par->set_fb; | |
480 | if (cur_fb && cur_fb->width == mode_cmd.width && | |
481 | cur_fb->height == mode_cmd.height && | |
482 | cur_fb->bits_per_pixel == mode_cmd.bpp && | |
483 | cur_fb->depth == mode_cmd.depth && | |
484 | cur_fb->pitches[0] == mode_cmd.pitch) | |
485 | return 0; | |
486 | ||
487 | /* Need new buffer object ? */ | |
488 | new_bo_size = (size_t) mode_cmd.pitch * (size_t) mode_cmd.height; | |
489 | ret = vmw_fb_kms_detach(par, | |
490 | par->bo_size < new_bo_size || | |
491 | par->bo_size > 2*new_bo_size, | |
492 | true); | |
493 | if (ret) | |
494 | return ret; | |
495 | ||
496 | if (!par->vmw_bo) { | |
497 | ret = vmw_fb_create_bo(par->vmw_priv, new_bo_size, | |
498 | &par->vmw_bo); | |
499 | if (ret) { | |
500 | DRM_ERROR("Failed creating a buffer object for " | |
501 | "fbdev.\n"); | |
502 | return ret; | |
503 | } | |
504 | par->bo_size = new_bo_size; | |
505 | } | |
506 | ||
507 | vfb = vmw_kms_new_framebuffer(par->vmw_priv, par->vmw_bo, NULL, | |
508 | true, &mode_cmd); | |
509 | if (IS_ERR(vfb)) | |
510 | return PTR_ERR(vfb); | |
511 | ||
512 | par->set_fb = &vfb->base; | |
513 | ||
514 | if (!par->bo_ptr) { | |
515 | /* | |
516 | * Pin before mapping. Since we don't know in what placement | |
517 | * to pin, call into KMS to do it for us. | |
518 | */ | |
519 | ret = vfb->pin(vfb); | |
520 | if (ret) { | |
521 | DRM_ERROR("Could not pin the fbdev framebuffer.\n"); | |
522 | return ret; | |
523 | } | |
524 | ||
525 | ret = ttm_bo_kmap(&par->vmw_bo->base, 0, | |
526 | par->vmw_bo->base.num_pages, &par->map); | |
527 | if (ret) { | |
528 | vfb->unpin(vfb); | |
529 | DRM_ERROR("Could not map the fbdev framebuffer.\n"); | |
530 | return ret; | |
531 | } | |
532 | ||
533 | par->bo_ptr = ttm_kmap_obj_virtual(&par->map, &par->bo_iowrite); | |
534 | } | |
535 | ||
536 | return 0; | |
537 | } | |
538 | ||
539 | static int vmw_fb_set_par(struct fb_info *info) | |
540 | { | |
541 | struct vmw_fb_par *par = info->par; | |
542 | struct vmw_private *vmw_priv = par->vmw_priv; | |
543 | struct drm_mode_set set; | |
544 | struct fb_var_screeninfo *var = &info->var; | |
545 | struct drm_display_mode new_mode = { DRM_MODE("fb_mode", | |
546 | DRM_MODE_TYPE_DRIVER, | |
547 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
548 | DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) | |
549 | }; | |
550 | struct drm_display_mode *old_mode; | |
551 | struct drm_display_mode *mode; | |
552 | int ret; | |
553 | ||
554 | old_mode = par->set_mode; | |
555 | mode = drm_mode_duplicate(vmw_priv->dev, &new_mode); | |
556 | if (!mode) { | |
557 | DRM_ERROR("Could not create new fb mode.\n"); | |
558 | return -ENOMEM; | |
559 | } | |
560 | ||
561 | mode->hdisplay = var->xres; | |
562 | mode->vdisplay = var->yres; | |
563 | vmw_guess_mode_timing(mode); | |
564 | ||
565 | if (old_mode && drm_mode_equal(old_mode, mode)) { | |
566 | drm_mode_destroy(vmw_priv->dev, mode); | |
567 | mode = old_mode; | |
568 | old_mode = NULL; | |
569 | } else if (!vmw_kms_validate_mode_vram(vmw_priv, | |
570 | mode->hdisplay * | |
571 | (var->bits_per_pixel + 7) / 8, | |
572 | mode->vdisplay)) { | |
573 | drm_mode_destroy(vmw_priv->dev, mode); | |
574 | return -EINVAL; | |
575 | } | |
576 | ||
577 | mutex_lock(&par->bo_mutex); | |
578 | drm_modeset_lock_all(vmw_priv->dev); | |
579 | ret = vmw_fb_kms_framebuffer(info); | |
580 | if (ret) | |
581 | goto out_unlock; | |
582 | ||
583 | par->fb_x = var->xoffset; | |
584 | par->fb_y = var->yoffset; | |
585 | ||
586 | set.crtc = par->crtc; | |
587 | set.x = 0; | |
588 | set.y = 0; | |
589 | set.mode = mode; | |
590 | set.fb = par->set_fb; | |
591 | set.num_connectors = 1; | |
592 | set.connectors = &par->con; | |
593 | ||
594 | ret = drm_mode_set_config_internal(&set); | |
595 | if (ret) | |
596 | goto out_unlock; | |
597 | ||
598 | vmw_fb_dirty_mark(par, par->fb_x, par->fb_y, | |
599 | par->set_fb->width, par->set_fb->height); | |
600 | ||
601 | /* If there already was stuff dirty we wont | |
602 | * schedule a new work, so lets do it now */ | |
603 | ||
604 | #if (defined(VMWGFX_STANDALONE) && defined(VMWGFX_FB_DEFERRED)) | |
605 | schedule_delayed_work(&par->def_par.deferred_work, 0); | |
606 | #else | |
607 | schedule_delayed_work(&info->deferred_work, 0); | |
608 | #endif | |
609 | ||
610 | out_unlock: | |
611 | if (old_mode) | |
612 | drm_mode_destroy(vmw_priv->dev, old_mode); | |
613 | par->set_mode = mode; | |
614 | ||
615 | drm_modeset_unlock_all(vmw_priv->dev); | |
616 | mutex_unlock(&par->bo_mutex); | |
617 | ||
618 | return ret; | |
619 | } | |
620 | ||
621 | ||
622 | static struct fb_ops vmw_fb_ops = { | |
623 | .owner = THIS_MODULE, | |
624 | .fb_check_var = vmw_fb_check_var, | |
625 | .fb_set_par = vmw_fb_set_par, | |
626 | .fb_setcolreg = vmw_fb_setcolreg, | |
627 | .fb_fillrect = vmw_fb_fillrect, | |
628 | .fb_copyarea = vmw_fb_copyarea, | |
629 | .fb_imageblit = vmw_fb_imageblit, | |
630 | .fb_pan_display = vmw_fb_pan_display, | |
631 | .fb_blank = vmw_fb_blank, | |
632 | }; | |
633 | ||
fb1d9738 JB |
634 | int vmw_fb_init(struct vmw_private *vmw_priv) |
635 | { | |
636 | struct device *device = &vmw_priv->dev->pdev->dev; | |
637 | struct vmw_fb_par *par; | |
638 | struct fb_info *info; | |
fb1d9738 | 639 | unsigned fb_width, fb_height; |
6558429b | 640 | unsigned fb_bpp, fb_depth, fb_offset, fb_pitch, fb_size; |
a278724a | 641 | struct drm_display_mode *init_mode; |
fb1d9738 JB |
642 | int ret; |
643 | ||
6558429b | 644 | fb_bpp = 32; |
fb1d9738 JB |
645 | fb_depth = 24; |
646 | ||
d7e1958d JB |
647 | /* XXX As shouldn't these be as well. */ |
648 | fb_width = min(vmw_priv->fb_max_width, (unsigned)2048); | |
649 | fb_height = min(vmw_priv->fb_max_height, (unsigned)2048); | |
fb1d9738 | 650 | |
6558429b | 651 | fb_pitch = fb_width * fb_bpp / 8; |
d7e1958d | 652 | fb_size = fb_pitch * fb_height; |
fb1d9738 | 653 | fb_offset = vmw_read(vmw_priv, SVGA_REG_FB_OFFSET); |
fb1d9738 JB |
654 | |
655 | info = framebuffer_alloc(sizeof(*par), device); | |
656 | if (!info) | |
657 | return -ENOMEM; | |
658 | ||
659 | /* | |
660 | * Par | |
661 | */ | |
662 | vmw_priv->fb_info = info; | |
663 | par = info->par; | |
a278724a | 664 | memset(par, 0, sizeof(*par)); |
fb1d9738 | 665 | par->vmw_priv = vmw_priv; |
fb1d9738 JB |
666 | par->vmalloc = NULL; |
667 | par->max_width = fb_width; | |
668 | par->max_height = fb_height; | |
669 | ||
a278724a TH |
670 | drm_modeset_lock_all(vmw_priv->dev); |
671 | ret = vmw_kms_fbdev_init_data(vmw_priv, 0, par->max_width, | |
672 | par->max_height, &par->con, | |
673 | &par->crtc, &init_mode); | |
674 | if (ret) { | |
675 | drm_modeset_unlock_all(vmw_priv->dev); | |
676 | goto err_kms; | |
677 | } | |
678 | ||
679 | info->var.xres = init_mode->hdisplay; | |
680 | info->var.yres = init_mode->vdisplay; | |
681 | drm_modeset_unlock_all(vmw_priv->dev); | |
682 | ||
fb1d9738 JB |
683 | /* |
684 | * Create buffers and alloc memory | |
685 | */ | |
a278724a | 686 | par->vmalloc = vzalloc(fb_size); |
fb1d9738 JB |
687 | if (unlikely(par->vmalloc == NULL)) { |
688 | ret = -ENOMEM; | |
689 | goto err_free; | |
690 | } | |
691 | ||
fb1d9738 JB |
692 | /* |
693 | * Fixed and var | |
694 | */ | |
695 | strcpy(info->fix.id, "svgadrmfb"); | |
696 | info->fix.type = FB_TYPE_PACKED_PIXELS; | |
697 | info->fix.visual = FB_VISUAL_TRUECOLOR; | |
698 | info->fix.type_aux = 0; | |
699 | info->fix.xpanstep = 1; /* doing it in hw */ | |
700 | info->fix.ypanstep = 1; /* doing it in hw */ | |
701 | info->fix.ywrapstep = 0; | |
702 | info->fix.accel = FB_ACCEL_NONE; | |
703 | info->fix.line_length = fb_pitch; | |
704 | ||
705 | info->fix.smem_start = 0; | |
706 | info->fix.smem_len = fb_size; | |
707 | ||
fb1d9738 | 708 | info->pseudo_palette = par->pseudo_palette; |
b9eb1a61 | 709 | info->screen_base = (char __iomem *)par->vmalloc; |
fb1d9738 JB |
710 | info->screen_size = fb_size; |
711 | ||
712 | info->flags = FBINFO_DEFAULT; | |
713 | info->fbops = &vmw_fb_ops; | |
714 | ||
715 | /* 24 depth per default */ | |
716 | info->var.red.offset = 16; | |
717 | info->var.green.offset = 8; | |
718 | info->var.blue.offset = 0; | |
719 | info->var.red.length = 8; | |
720 | info->var.green.length = 8; | |
721 | info->var.blue.length = 8; | |
722 | info->var.transp.offset = 0; | |
723 | info->var.transp.length = 0; | |
724 | ||
725 | info->var.xres_virtual = fb_width; | |
726 | info->var.yres_virtual = fb_height; | |
a278724a | 727 | info->var.bits_per_pixel = fb_bpp; |
fb1d9738 JB |
728 | info->var.xoffset = 0; |
729 | info->var.yoffset = 0; | |
730 | info->var.activate = FB_ACTIVATE_NOW; | |
731 | info->var.height = -1; | |
732 | info->var.width = -1; | |
733 | ||
fb2a99e1 | 734 | /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */ |
1471ca9a MS |
735 | info->apertures = alloc_apertures(1); |
736 | if (!info->apertures) { | |
737 | ret = -ENOMEM; | |
738 | goto err_aper; | |
739 | } | |
740 | info->apertures->ranges[0].base = vmw_priv->vram_start; | |
741 | info->apertures->ranges[0].size = vmw_priv->vram_size; | |
f2d12b8e | 742 | |
fb1d9738 JB |
743 | /* |
744 | * Dirty & Deferred IO | |
745 | */ | |
746 | par->dirty.x1 = par->dirty.x2 = 0; | |
c39721c7 | 747 | par->dirty.y1 = par->dirty.y2 = 0; |
fb1d9738 JB |
748 | par->dirty.active = true; |
749 | spin_lock_init(&par->dirty.lock); | |
a278724a | 750 | mutex_init(&par->bo_mutex); |
fb1d9738 JB |
751 | info->fbdefio = &vmw_defio; |
752 | fb_deferred_io_init(info); | |
753 | ||
754 | ret = register_framebuffer(info); | |
755 | if (unlikely(ret != 0)) | |
756 | goto err_defio; | |
757 | ||
a278724a TH |
758 | vmw_fb_set_par(info); |
759 | ||
fb1d9738 JB |
760 | return 0; |
761 | ||
762 | err_defio: | |
763 | fb_deferred_io_cleanup(info); | |
1471ca9a | 764 | err_aper: |
fb1d9738 JB |
765 | err_free: |
766 | vfree(par->vmalloc); | |
a278724a | 767 | err_kms: |
fb1d9738 JB |
768 | framebuffer_release(info); |
769 | vmw_priv->fb_info = NULL; | |
770 | ||
771 | return ret; | |
772 | } | |
773 | ||
774 | int vmw_fb_close(struct vmw_private *vmw_priv) | |
775 | { | |
776 | struct fb_info *info; | |
777 | struct vmw_fb_par *par; | |
fb1d9738 JB |
778 | |
779 | if (!vmw_priv->fb_info) | |
780 | return 0; | |
781 | ||
782 | info = vmw_priv->fb_info; | |
783 | par = info->par; | |
fb1d9738 JB |
784 | |
785 | /* ??? order */ | |
786 | fb_deferred_io_cleanup(info); | |
787 | unregister_framebuffer(info); | |
788 | ||
a278724a | 789 | (void) vmw_fb_kms_detach(par, true, true); |
fb1d9738 JB |
790 | |
791 | vfree(par->vmalloc); | |
792 | framebuffer_release(info); | |
793 | ||
794 | return 0; | |
795 | } | |
796 | ||
fb1d9738 JB |
797 | int vmw_fb_off(struct vmw_private *vmw_priv) |
798 | { | |
799 | struct fb_info *info; | |
800 | struct vmw_fb_par *par; | |
801 | unsigned long flags; | |
802 | ||
803 | if (!vmw_priv->fb_info) | |
804 | return -EINVAL; | |
805 | ||
806 | info = vmw_priv->fb_info; | |
807 | par = info->par; | |
808 | ||
809 | spin_lock_irqsave(&par->dirty.lock, flags); | |
810 | par->dirty.active = false; | |
811 | spin_unlock_irqrestore(&par->dirty.lock, flags); | |
812 | ||
43829731 | 813 | flush_delayed_work(&info->deferred_work); |
fb1d9738 | 814 | |
a278724a TH |
815 | mutex_lock(&par->bo_mutex); |
816 | (void) vmw_fb_kms_detach(par, true, false); | |
817 | mutex_unlock(&par->bo_mutex); | |
fb1d9738 JB |
818 | |
819 | return 0; | |
820 | } | |
821 | ||
822 | int vmw_fb_on(struct vmw_private *vmw_priv) | |
823 | { | |
824 | struct fb_info *info; | |
825 | struct vmw_fb_par *par; | |
826 | unsigned long flags; | |
fb1d9738 JB |
827 | |
828 | if (!vmw_priv->fb_info) | |
829 | return -EINVAL; | |
830 | ||
831 | info = vmw_priv->fb_info; | |
832 | par = info->par; | |
833 | ||
a278724a | 834 | vmw_fb_set_par(info); |
fb1d9738 JB |
835 | spin_lock_irqsave(&par->dirty.lock, flags); |
836 | par->dirty.active = true; | |
837 | spin_unlock_irqrestore(&par->dirty.lock, flags); | |
a278724a | 838 | |
fb1d9738 JB |
839 | return 0; |
840 | } |