Commit | Line | Data |
---|---|---|
4d8d096e AC |
1 | /************************************************************************** |
2 | * Copyright (c) 2007-2011, Intel Corporation. | |
3 | * All Rights Reserved. | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms and conditions of the GNU General Public License, | |
7 | * version 2, as published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope it will be useful, but WITHOUT | |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
12 | * more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License along with | |
15 | * this program; if not, write to the Free Software Foundation, Inc., | |
16 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. | |
17 | * | |
18 | **************************************************************************/ | |
19 | ||
20 | #include <linux/module.h> | |
21 | #include <linux/kernel.h> | |
22 | #include <linux/errno.h> | |
23 | #include <linux/string.h> | |
24 | #include <linux/mm.h> | |
25 | #include <linux/tty.h> | |
26 | #include <linux/slab.h> | |
27 | #include <linux/delay.h> | |
28 | #include <linux/fb.h> | |
29 | #include <linux/init.h> | |
30 | #include <linux/console.h> | |
31 | ||
32 | #include <drm/drmP.h> | |
33 | #include <drm/drm.h> | |
34 | #include <drm/drm_crtc.h> | |
a9a644ac | 35 | #include <drm/drm_fb_helper.h> |
4d8d096e AC |
36 | |
37 | #include "psb_drv.h" | |
38 | #include "psb_intel_reg.h" | |
39 | #include "psb_intel_drv.h" | |
40 | #include "framebuffer.h" | |
41 | ||
42 | static void psb_user_framebuffer_destroy(struct drm_framebuffer *fb); | |
43 | static int psb_user_framebuffer_create_handle(struct drm_framebuffer *fb, | |
44 | struct drm_file *file_priv, | |
45 | unsigned int *handle); | |
46 | ||
47 | static const struct drm_framebuffer_funcs psb_fb_funcs = { | |
48 | .destroy = psb_user_framebuffer_destroy, | |
49 | .create_handle = psb_user_framebuffer_create_handle, | |
50 | }; | |
51 | ||
52 | #define CMAP_TOHW(_val, _width) ((((_val) << (_width)) + 0x7FFF - (_val)) >> 16) | |
53 | ||
54 | static int psbfb_setcolreg(unsigned regno, unsigned red, unsigned green, | |
55 | unsigned blue, unsigned transp, | |
56 | struct fb_info *info) | |
57 | { | |
58 | struct psb_fbdev *fbdev = info->par; | |
59 | struct drm_framebuffer *fb = fbdev->psb_fb_helper.fb; | |
60 | uint32_t v; | |
61 | ||
62 | if (!fb) | |
63 | return -ENOMEM; | |
64 | ||
65 | if (regno > 255) | |
66 | return 1; | |
67 | ||
68 | red = CMAP_TOHW(red, info->var.red.length); | |
69 | blue = CMAP_TOHW(blue, info->var.blue.length); | |
70 | green = CMAP_TOHW(green, info->var.green.length); | |
71 | transp = CMAP_TOHW(transp, info->var.transp.length); | |
72 | ||
73 | v = (red << info->var.red.offset) | | |
74 | (green << info->var.green.offset) | | |
75 | (blue << info->var.blue.offset) | | |
76 | (transp << info->var.transp.offset); | |
77 | ||
78 | if (regno < 16) { | |
79 | switch (fb->bits_per_pixel) { | |
80 | case 16: | |
81 | ((uint32_t *) info->pseudo_palette)[regno] = v; | |
82 | break; | |
83 | case 24: | |
84 | case 32: | |
85 | ((uint32_t *) info->pseudo_palette)[regno] = v; | |
86 | break; | |
87 | } | |
88 | } | |
89 | ||
90 | return 0; | |
91 | } | |
92 | ||
93 | ||
94 | void psbfb_suspend(struct drm_device *dev) | |
95 | { | |
96 | struct drm_framebuffer *fb = 0; | |
97 | struct psb_framebuffer *psbfb = to_psb_fb(fb); | |
98 | ||
99 | console_lock(); | |
100 | mutex_lock(&dev->mode_config.mutex); | |
101 | list_for_each_entry(fb, &dev->mode_config.fb_list, head) { | |
102 | struct fb_info *info = psbfb->fbdev; | |
103 | fb_set_suspend(info, 1); | |
104 | drm_fb_helper_blank(FB_BLANK_POWERDOWN, info); | |
105 | } | |
106 | mutex_unlock(&dev->mode_config.mutex); | |
107 | console_unlock(); | |
108 | } | |
109 | ||
110 | void psbfb_resume(struct drm_device *dev) | |
111 | { | |
112 | struct drm_framebuffer *fb = 0; | |
113 | struct psb_framebuffer *psbfb = to_psb_fb(fb); | |
114 | ||
115 | console_lock(); | |
116 | mutex_lock(&dev->mode_config.mutex); | |
117 | list_for_each_entry(fb, &dev->mode_config.fb_list, head) { | |
118 | struct fb_info *info = psbfb->fbdev; | |
119 | fb_set_suspend(info, 0); | |
120 | drm_fb_helper_blank(FB_BLANK_UNBLANK, info); | |
121 | } | |
122 | mutex_unlock(&dev->mode_config.mutex); | |
123 | console_unlock(); | |
124 | drm_helper_disable_unused_functions(dev); | |
125 | } | |
126 | ||
127 | static int psbfb_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) | |
128 | { | |
129 | struct psb_framebuffer *psbfb = vma->vm_private_data; | |
130 | struct drm_device *dev = psbfb->base.dev; | |
131 | struct drm_psb_private *dev_priv = dev->dev_private; | |
132 | int page_num; | |
133 | int i; | |
134 | unsigned long address; | |
135 | int ret; | |
136 | unsigned long pfn; | |
137 | /* FIXME: assumes fb at stolen base which may not be true */ | |
138 | unsigned long phys_addr = (unsigned long)dev_priv->stolen_base; | |
139 | ||
140 | page_num = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT; | |
141 | address = (unsigned long)vmf->virtual_address; | |
142 | ||
143 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); | |
144 | ||
145 | for (i = 0; i < page_num; i++) { | |
146 | pfn = (phys_addr >> PAGE_SHIFT); | |
147 | ||
148 | ret = vm_insert_mixed(vma, address, pfn); | |
149 | if (unlikely((ret == -EBUSY) || (ret != 0 && i > 0))) | |
150 | break; | |
151 | else if (unlikely(ret != 0)) { | |
152 | ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS; | |
153 | return ret; | |
154 | } | |
155 | address += PAGE_SIZE; | |
156 | phys_addr += PAGE_SIZE; | |
157 | } | |
158 | return VM_FAULT_NOPAGE; | |
159 | } | |
160 | ||
161 | static void psbfb_vm_open(struct vm_area_struct *vma) | |
162 | { | |
163 | } | |
164 | ||
165 | static void psbfb_vm_close(struct vm_area_struct *vma) | |
166 | { | |
167 | } | |
168 | ||
169 | static struct vm_operations_struct psbfb_vm_ops = { | |
170 | .fault = psbfb_vm_fault, | |
171 | .open = psbfb_vm_open, | |
172 | .close = psbfb_vm_close | |
173 | }; | |
174 | ||
175 | static int psbfb_mmap(struct fb_info *info, struct vm_area_struct *vma) | |
176 | { | |
177 | struct psb_fbdev *fbdev = info->par; | |
178 | struct psb_framebuffer *psbfb = &fbdev->pfb; | |
179 | ||
180 | if (vma->vm_pgoff != 0) | |
181 | return -EINVAL; | |
182 | if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT)) | |
183 | return -EINVAL; | |
184 | ||
185 | if (!psbfb->addr_space) | |
186 | psbfb->addr_space = vma->vm_file->f_mapping; | |
187 | /* | |
188 | * If this is a GEM object then info->screen_base is the virtual | |
189 | * kernel remapping of the object. FIXME: Review if this is | |
190 | * suitable for our mmap work | |
191 | */ | |
192 | vma->vm_ops = &psbfb_vm_ops; | |
193 | vma->vm_private_data = (void *)psbfb; | |
194 | vma->vm_flags |= VM_RESERVED | VM_IO | | |
195 | VM_MIXEDMAP | VM_DONTEXPAND; | |
196 | return 0; | |
197 | } | |
198 | ||
199 | static int psbfb_ioctl(struct fb_info *info, unsigned int cmd, | |
200 | unsigned long arg) | |
201 | { | |
202 | return -ENOTTY; | |
203 | } | |
204 | ||
205 | static struct fb_ops psbfb_ops = { | |
206 | .owner = THIS_MODULE, | |
207 | .fb_check_var = drm_fb_helper_check_var, | |
208 | .fb_set_par = drm_fb_helper_set_par, | |
209 | .fb_blank = drm_fb_helper_blank, | |
210 | .fb_setcolreg = psbfb_setcolreg, | |
211 | .fb_fillrect = cfb_fillrect, | |
212 | .fb_copyarea = psbfb_copyarea, | |
213 | .fb_imageblit = cfb_imageblit, | |
214 | .fb_mmap = psbfb_mmap, | |
215 | .fb_sync = psbfb_sync, | |
216 | .fb_ioctl = psbfb_ioctl, | |
217 | }; | |
218 | ||
219 | static struct fb_ops psbfb_unaccel_ops = { | |
220 | .owner = THIS_MODULE, | |
221 | .fb_check_var = drm_fb_helper_check_var, | |
222 | .fb_set_par = drm_fb_helper_set_par, | |
223 | .fb_blank = drm_fb_helper_blank, | |
224 | .fb_setcolreg = psbfb_setcolreg, | |
225 | .fb_fillrect = cfb_fillrect, | |
226 | .fb_copyarea = cfb_copyarea, | |
227 | .fb_imageblit = cfb_imageblit, | |
228 | .fb_mmap = psbfb_mmap, | |
229 | .fb_ioctl = psbfb_ioctl, | |
230 | }; | |
231 | ||
232 | /** | |
233 | * psb_framebuffer_init - initialize a framebuffer | |
234 | * @dev: our DRM device | |
235 | * @fb: framebuffer to set up | |
236 | * @mode_cmd: mode description | |
237 | * @gt: backing object | |
238 | * | |
239 | * Configure and fill in the boilerplate for our frame buffer. Return | |
240 | * 0 on success or an error code if we fail. | |
241 | */ | |
242 | static int psb_framebuffer_init(struct drm_device *dev, | |
243 | struct psb_framebuffer *fb, | |
a9a644ac | 244 | struct drm_mode_fb_cmd2 *mode_cmd, |
4d8d096e AC |
245 | struct gtt_range *gt) |
246 | { | |
a9a644ac | 247 | u32 bpp, depth; |
4d8d096e AC |
248 | int ret; |
249 | ||
a9a644ac DA |
250 | drm_helper_get_fb_bpp_depth(mode_cmd->pixel_format, &depth, &bpp); |
251 | ||
252 | if (mode_cmd->pitches[0] & 63) | |
4d8d096e | 253 | return -EINVAL; |
a9a644ac | 254 | switch (bpp) { |
4d8d096e AC |
255 | case 8: |
256 | case 16: | |
257 | case 24: | |
258 | case 32: | |
259 | break; | |
260 | default: | |
261 | return -EINVAL; | |
262 | } | |
263 | ret = drm_framebuffer_init(dev, &fb->base, &psb_fb_funcs); | |
264 | if (ret) { | |
265 | dev_err(dev->dev, "framebuffer init failed: %d\n", ret); | |
266 | return ret; | |
267 | } | |
268 | drm_helper_mode_fill_fb_struct(&fb->base, mode_cmd); | |
269 | fb->gtt = gt; | |
270 | return 0; | |
271 | } | |
272 | ||
273 | /** | |
274 | * psb_framebuffer_create - create a framebuffer backed by gt | |
275 | * @dev: our DRM device | |
276 | * @mode_cmd: the description of the requested mode | |
277 | * @gt: the backing object | |
278 | * | |
279 | * Create a framebuffer object backed by the gt, and fill in the | |
280 | * boilerplate required | |
281 | * | |
282 | * TODO: review object references | |
283 | */ | |
284 | ||
285 | static struct drm_framebuffer *psb_framebuffer_create | |
286 | (struct drm_device *dev, | |
a9a644ac | 287 | struct drm_mode_fb_cmd2 *mode_cmd, |
4d8d096e AC |
288 | struct gtt_range *gt) |
289 | { | |
290 | struct psb_framebuffer *fb; | |
291 | int ret; | |
292 | ||
293 | fb = kzalloc(sizeof(*fb), GFP_KERNEL); | |
294 | if (!fb) | |
295 | return ERR_PTR(-ENOMEM); | |
296 | ||
297 | ret = psb_framebuffer_init(dev, fb, mode_cmd, gt); | |
298 | if (ret) { | |
299 | kfree(fb); | |
300 | return ERR_PTR(ret); | |
301 | } | |
302 | return &fb->base; | |
303 | } | |
304 | ||
305 | /** | |
306 | * psbfb_alloc - allocate frame buffer memory | |
307 | * @dev: the DRM device | |
308 | * @aligned_size: space needed | |
309 | * | |
310 | * Allocate the frame buffer. In the usual case we get a GTT range that | |
311 | * is stolen memory backed and life is simple. If there isn't sufficient | |
312 | * stolen memory or the system has no stolen memory we allocate a range | |
313 | * and back it with a GEM object. | |
314 | * | |
315 | * In this case the GEM object has no handle. | |
316 | * | |
317 | * FIXME: console speed up - allocate twice the space if room and use | |
318 | * hardware scrolling for acceleration. | |
319 | */ | |
320 | static struct gtt_range *psbfb_alloc(struct drm_device *dev, int aligned_size) | |
321 | { | |
322 | struct gtt_range *backing; | |
323 | /* Begin by trying to use stolen memory backing */ | |
324 | backing = psb_gtt_alloc_range(dev, aligned_size, "fb", 1); | |
325 | if (backing) { | |
326 | if (drm_gem_private_object_init(dev, | |
327 | &backing->gem, aligned_size) == 0) | |
328 | return backing; | |
329 | psb_gtt_free_range(dev, backing); | |
330 | } | |
331 | /* Next try using GEM host memory */ | |
332 | backing = psb_gtt_alloc_range(dev, aligned_size, "fb(gem)", 0); | |
333 | if (backing == NULL) | |
334 | return NULL; | |
335 | ||
336 | /* Now back it with an object */ | |
337 | if (drm_gem_object_init(dev, &backing->gem, aligned_size) != 0) { | |
338 | psb_gtt_free_range(dev, backing); | |
339 | return NULL; | |
340 | } | |
341 | return backing; | |
342 | } | |
343 | ||
344 | /** | |
345 | * psbfb_create - create a framebuffer | |
346 | * @fbdev: the framebuffer device | |
347 | * @sizes: specification of the layout | |
348 | * | |
349 | * Create a framebuffer to the specifications provided | |
350 | */ | |
351 | static int psbfb_create(struct psb_fbdev *fbdev, | |
352 | struct drm_fb_helper_surface_size *sizes) | |
353 | { | |
354 | struct drm_device *dev = fbdev->psb_fb_helper.dev; | |
355 | struct drm_psb_private *dev_priv = dev->dev_private; | |
356 | struct fb_info *info; | |
357 | struct drm_framebuffer *fb; | |
358 | struct psb_framebuffer *psbfb = &fbdev->pfb; | |
a9a644ac | 359 | struct drm_mode_fb_cmd2 mode_cmd; |
4d8d096e AC |
360 | struct device *device = &dev->pdev->dev; |
361 | int size; | |
362 | int ret; | |
363 | struct gtt_range *backing; | |
a9a644ac | 364 | u32 bpp, depth; |
4d8d096e AC |
365 | |
366 | mode_cmd.width = sizes->surface_width; | |
367 | mode_cmd.height = sizes->surface_height; | |
a9a644ac | 368 | bpp = sizes->surface_bpp; |
4d8d096e AC |
369 | |
370 | /* No 24bit packed */ | |
a9a644ac DA |
371 | if (bpp == 24) |
372 | bpp = 32; | |
4d8d096e AC |
373 | |
374 | /* HW requires pitch to be 64 byte aligned */ | |
a9a644ac DA |
375 | mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 7) / 8), 64); |
376 | depth = sizes->surface_depth; | |
4d8d096e | 377 | |
a9a644ac | 378 | size = mode_cmd.pitches[0] * mode_cmd.height; |
4d8d096e AC |
379 | size = ALIGN(size, PAGE_SIZE); |
380 | ||
381 | /* Allocate the framebuffer in the GTT with stolen page backing */ | |
382 | backing = psbfb_alloc(dev, size); | |
383 | if (backing == NULL) | |
384 | return -ENOMEM; | |
385 | ||
386 | mutex_lock(&dev->struct_mutex); | |
387 | ||
388 | info = framebuffer_alloc(0, device); | |
389 | if (!info) { | |
390 | ret = -ENOMEM; | |
391 | goto out_err1; | |
392 | } | |
393 | info->par = fbdev; | |
394 | ||
a9a644ac DA |
395 | mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth); |
396 | ||
4d8d096e AC |
397 | ret = psb_framebuffer_init(dev, psbfb, &mode_cmd, backing); |
398 | if (ret) | |
399 | goto out_unref; | |
400 | ||
401 | fb = &psbfb->base; | |
402 | psbfb->fbdev = info; | |
403 | ||
404 | fbdev->psb_fb_helper.fb = fb; | |
405 | fbdev->psb_fb_helper.fbdev = info; | |
406 | ||
407 | strcpy(info->fix.id, "psbfb"); | |
408 | ||
409 | info->flags = FBINFO_DEFAULT; | |
410 | /* No 2D engine */ | |
411 | if (!dev_priv->ops->accel_2d) | |
412 | info->fbops = &psbfb_unaccel_ops; | |
413 | else | |
414 | info->fbops = &psbfb_ops; | |
415 | ||
416 | ret = fb_alloc_cmap(&info->cmap, 256, 0); | |
417 | if (ret) { | |
418 | ret = -ENOMEM; | |
419 | goto out_unref; | |
420 | } | |
421 | ||
422 | info->fix.smem_start = dev->mode_config.fb_base; | |
423 | info->fix.smem_len = size; | |
424 | ||
425 | if (backing->stolen) { | |
426 | /* Accessed stolen memory directly */ | |
427 | info->screen_base = (char *)dev_priv->vram_addr + | |
428 | backing->offset; | |
429 | } else { | |
430 | /* Pin the pages into the GTT and create a mapping to them */ | |
431 | psb_gtt_pin(backing); | |
432 | info->screen_base = vm_map_ram(backing->pages, backing->npage, | |
433 | -1, PAGE_KERNEL); | |
434 | if (info->screen_base == NULL) { | |
435 | psb_gtt_unpin(backing); | |
436 | ret = -ENOMEM; | |
437 | goto out_unref; | |
438 | } | |
439 | psbfb->vm_map = 1; | |
440 | } | |
441 | info->screen_size = size; | |
442 | ||
443 | if (dev_priv->gtt.stolen_size) { | |
444 | info->apertures = alloc_apertures(1); | |
445 | if (!info->apertures) { | |
446 | ret = -ENOMEM; | |
447 | goto out_unref; | |
448 | } | |
449 | info->apertures->ranges[0].base = dev->mode_config.fb_base; | |
450 | info->apertures->ranges[0].size = dev_priv->gtt.stolen_size; | |
451 | } | |
452 | ||
453 | drm_fb_helper_fill_fix(info, fb->pitch, fb->depth); | |
454 | drm_fb_helper_fill_var(info, &fbdev->psb_fb_helper, | |
455 | sizes->fb_width, sizes->fb_height); | |
456 | ||
457 | info->fix.mmio_start = pci_resource_start(dev->pdev, 0); | |
458 | info->fix.mmio_len = pci_resource_len(dev->pdev, 0); | |
459 | ||
460 | info->pixmap.size = 64 * 1024; | |
461 | info->pixmap.buf_align = 8; | |
462 | info->pixmap.access_align = 32; | |
463 | info->pixmap.flags = FB_PIXMAP_SYSTEM; | |
464 | info->pixmap.scan_align = 1; | |
465 | ||
466 | dev_info(dev->dev, "allocated %dx%d fb\n", | |
467 | psbfb->base.width, psbfb->base.height); | |
468 | ||
469 | mutex_unlock(&dev->struct_mutex); | |
470 | return 0; | |
471 | out_unref: | |
472 | if (backing->stolen) | |
473 | psb_gtt_free_range(dev, backing); | |
474 | else { | |
475 | if (psbfb->vm_map) | |
476 | vm_unmap_ram(info->screen_base, backing->npage); | |
477 | drm_gem_object_unreference(&backing->gem); | |
478 | } | |
479 | out_err1: | |
480 | mutex_unlock(&dev->struct_mutex); | |
481 | psb_gtt_free_range(dev, backing); | |
482 | return ret; | |
483 | } | |
484 | ||
485 | /** | |
486 | * psb_user_framebuffer_create - create framebuffer | |
487 | * @dev: our DRM device | |
488 | * @filp: client file | |
489 | * @cmd: mode request | |
490 | * | |
491 | * Create a new framebuffer backed by a userspace GEM object | |
492 | */ | |
493 | static struct drm_framebuffer *psb_user_framebuffer_create | |
494 | (struct drm_device *dev, struct drm_file *filp, | |
a9a644ac | 495 | struct drm_mode_fb_cmd2 *cmd) |
4d8d096e AC |
496 | { |
497 | struct gtt_range *r; | |
498 | struct drm_gem_object *obj; | |
499 | ||
500 | /* | |
501 | * Find the GEM object and thus the gtt range object that is | |
502 | * to back this space | |
503 | */ | |
a9a644ac | 504 | obj = drm_gem_object_lookup(dev, filp, cmd->handles[0]); |
4d8d096e AC |
505 | if (obj == NULL) |
506 | return ERR_PTR(-ENOENT); | |
507 | ||
508 | /* Let the core code do all the work */ | |
509 | r = container_of(obj, struct gtt_range, gem); | |
510 | return psb_framebuffer_create(dev, cmd, r); | |
511 | } | |
512 | ||
513 | static void psbfb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green, | |
514 | u16 blue, int regno) | |
515 | { | |
516 | } | |
517 | ||
518 | static void psbfb_gamma_get(struct drm_crtc *crtc, u16 *red, | |
519 | u16 *green, u16 *blue, int regno) | |
520 | { | |
521 | } | |
522 | ||
523 | static int psbfb_probe(struct drm_fb_helper *helper, | |
524 | struct drm_fb_helper_surface_size *sizes) | |
525 | { | |
526 | struct psb_fbdev *psb_fbdev = (struct psb_fbdev *)helper; | |
527 | int new_fb = 0; | |
528 | int ret; | |
529 | ||
530 | if (!helper->fb) { | |
531 | ret = psbfb_create(psb_fbdev, sizes); | |
532 | if (ret) | |
533 | return ret; | |
534 | new_fb = 1; | |
535 | } | |
536 | return new_fb; | |
537 | } | |
538 | ||
539 | struct drm_fb_helper_funcs psb_fb_helper_funcs = { | |
540 | .gamma_set = psbfb_gamma_set, | |
541 | .gamma_get = psbfb_gamma_get, | |
542 | .fb_probe = psbfb_probe, | |
543 | }; | |
544 | ||
545 | int psb_fbdev_destroy(struct drm_device *dev, struct psb_fbdev *fbdev) | |
546 | { | |
547 | struct fb_info *info; | |
548 | struct psb_framebuffer *psbfb = &fbdev->pfb; | |
549 | ||
550 | if (fbdev->psb_fb_helper.fbdev) { | |
551 | info = fbdev->psb_fb_helper.fbdev; | |
552 | ||
553 | /* If this is our base framebuffer then kill any virtual map | |
554 | for the framebuffer layer and unpin it */ | |
555 | if (psbfb->vm_map) { | |
556 | vm_unmap_ram(info->screen_base, psbfb->gtt->npage); | |
557 | psb_gtt_unpin(psbfb->gtt); | |
558 | } | |
559 | unregister_framebuffer(info); | |
560 | if (info->cmap.len) | |
561 | fb_dealloc_cmap(&info->cmap); | |
562 | framebuffer_release(info); | |
563 | } | |
564 | drm_fb_helper_fini(&fbdev->psb_fb_helper); | |
565 | drm_framebuffer_cleanup(&psbfb->base); | |
566 | ||
567 | if (psbfb->gtt) | |
568 | drm_gem_object_unreference(&psbfb->gtt->gem); | |
569 | return 0; | |
570 | } | |
571 | ||
572 | int psb_fbdev_init(struct drm_device *dev) | |
573 | { | |
574 | struct psb_fbdev *fbdev; | |
575 | struct drm_psb_private *dev_priv = dev->dev_private; | |
576 | ||
577 | fbdev = kzalloc(sizeof(struct psb_fbdev), GFP_KERNEL); | |
578 | if (!fbdev) { | |
579 | dev_err(dev->dev, "no memory\n"); | |
580 | return -ENOMEM; | |
581 | } | |
582 | ||
583 | dev_priv->fbdev = fbdev; | |
584 | fbdev->psb_fb_helper.funcs = &psb_fb_helper_funcs; | |
585 | ||
586 | drm_fb_helper_init(dev, &fbdev->psb_fb_helper, dev_priv->ops->crtcs, | |
587 | INTELFB_CONN_LIMIT); | |
588 | ||
589 | drm_fb_helper_single_add_all_connectors(&fbdev->psb_fb_helper); | |
590 | drm_fb_helper_initial_config(&fbdev->psb_fb_helper, 32); | |
591 | return 0; | |
592 | } | |
593 | ||
594 | void psb_fbdev_fini(struct drm_device *dev) | |
595 | { | |
596 | struct drm_psb_private *dev_priv = dev->dev_private; | |
597 | ||
598 | if (!dev_priv->fbdev) | |
599 | return; | |
600 | ||
601 | psb_fbdev_destroy(dev, dev_priv->fbdev); | |
602 | kfree(dev_priv->fbdev); | |
603 | dev_priv->fbdev = NULL; | |
604 | } | |
605 | ||
606 | static void psbfb_output_poll_changed(struct drm_device *dev) | |
607 | { | |
608 | struct drm_psb_private *dev_priv = dev->dev_private; | |
609 | struct psb_fbdev *fbdev = (struct psb_fbdev *)dev_priv->fbdev; | |
610 | drm_fb_helper_hotplug_event(&fbdev->psb_fb_helper); | |
611 | } | |
612 | ||
613 | /** | |
614 | * psb_user_framebuffer_create_handle - add hamdle to a framebuffer | |
615 | * @fb: framebuffer | |
616 | * @file_priv: our DRM file | |
617 | * @handle: returned handle | |
618 | * | |
619 | * Our framebuffer object is a GTT range which also contains a GEM | |
620 | * object. We need to turn it into a handle for userspace. GEM will do | |
621 | * the work for us | |
622 | */ | |
623 | static int psb_user_framebuffer_create_handle(struct drm_framebuffer *fb, | |
624 | struct drm_file *file_priv, | |
625 | unsigned int *handle) | |
626 | { | |
627 | struct psb_framebuffer *psbfb = to_psb_fb(fb); | |
628 | struct gtt_range *r = psbfb->gtt; | |
629 | return drm_gem_handle_create(file_priv, &r->gem, handle); | |
630 | } | |
631 | ||
632 | /** | |
633 | * psb_user_framebuffer_destroy - destruct user created fb | |
634 | * @fb: framebuffer | |
635 | * | |
636 | * User framebuffers are backed by GEM objects so all we have to do is | |
637 | * clean up a bit and drop the reference, GEM will handle the fallout | |
638 | */ | |
639 | static void psb_user_framebuffer_destroy(struct drm_framebuffer *fb) | |
640 | { | |
641 | struct psb_framebuffer *psbfb = to_psb_fb(fb); | |
642 | struct gtt_range *r = psbfb->gtt; | |
643 | struct drm_device *dev = fb->dev; | |
644 | struct drm_psb_private *dev_priv = dev->dev_private; | |
645 | struct psb_fbdev *fbdev = dev_priv->fbdev; | |
646 | struct drm_crtc *crtc; | |
647 | int reset = 0; | |
648 | ||
649 | /* Should never get stolen memory for a user fb */ | |
650 | WARN_ON(r->stolen); | |
651 | ||
652 | /* Check if we are erroneously live */ | |
653 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) | |
654 | if (crtc->fb == fb) | |
655 | reset = 1; | |
656 | ||
657 | if (reset) | |
658 | /* | |
659 | * Now force a sane response before we permit the DRM CRTC | |
660 | * layer to do stupid things like blank the display. Instead | |
661 | * we reset this framebuffer as if the user had forced a reset. | |
662 | * We must do this before the cleanup so that the DRM layer | |
663 | * doesn't get a chance to stick its oar in where it isn't | |
664 | * wanted. | |
665 | */ | |
666 | drm_fb_helper_restore_fbdev_mode(&fbdev->psb_fb_helper); | |
667 | ||
668 | /* Let DRM do its clean up */ | |
669 | drm_framebuffer_cleanup(fb); | |
670 | /* We are no longer using the resource in GEM */ | |
671 | drm_gem_object_unreference_unlocked(&r->gem); | |
672 | kfree(fb); | |
673 | } | |
674 | ||
675 | static const struct drm_mode_config_funcs psb_mode_funcs = { | |
676 | .fb_create = psb_user_framebuffer_create, | |
677 | .output_poll_changed = psbfb_output_poll_changed, | |
678 | }; | |
679 | ||
680 | static int psb_create_backlight_property(struct drm_device *dev) | |
681 | { | |
682 | struct drm_psb_private *dev_priv = dev->dev_private; | |
683 | struct drm_property *backlight; | |
684 | ||
685 | if (dev_priv->backlight_property) | |
686 | return 0; | |
687 | ||
688 | backlight = drm_property_create(dev, DRM_MODE_PROP_RANGE, | |
689 | "backlight", 2); | |
690 | backlight->values[0] = 0; | |
691 | backlight->values[1] = 100; | |
692 | ||
693 | dev_priv->backlight_property = backlight; | |
694 | ||
695 | return 0; | |
696 | } | |
697 | ||
698 | static void psb_setup_outputs(struct drm_device *dev) | |
699 | { | |
700 | struct drm_psb_private *dev_priv = dev->dev_private; | |
701 | struct drm_connector *connector; | |
702 | ||
703 | drm_mode_create_scaling_mode_property(dev); | |
704 | psb_create_backlight_property(dev); | |
705 | ||
706 | dev_priv->ops->output_init(dev); | |
707 | ||
708 | list_for_each_entry(connector, &dev->mode_config.connector_list, | |
709 | head) { | |
710 | struct psb_intel_output *psb_intel_output = | |
711 | to_psb_intel_output(connector); | |
712 | struct drm_encoder *encoder = &psb_intel_output->enc; | |
713 | int crtc_mask = 0, clone_mask = 0; | |
714 | ||
715 | /* valid crtcs */ | |
716 | switch (psb_intel_output->type) { | |
717 | case INTEL_OUTPUT_ANALOG: | |
718 | crtc_mask = (1 << 0); | |
719 | clone_mask = (1 << INTEL_OUTPUT_ANALOG); | |
720 | break; | |
721 | case INTEL_OUTPUT_SDVO: | |
722 | crtc_mask = ((1 << 0) | (1 << 1)); | |
723 | clone_mask = (1 << INTEL_OUTPUT_SDVO); | |
724 | break; | |
725 | case INTEL_OUTPUT_LVDS: | |
726 | if (IS_MRST(dev)) | |
727 | crtc_mask = (1 << 0); | |
728 | else | |
729 | crtc_mask = (1 << 1); | |
730 | clone_mask = (1 << INTEL_OUTPUT_LVDS); | |
731 | break; | |
732 | case INTEL_OUTPUT_MIPI: | |
733 | crtc_mask = (1 << 0); | |
734 | clone_mask = (1 << INTEL_OUTPUT_MIPI); | |
735 | break; | |
736 | case INTEL_OUTPUT_MIPI2: | |
737 | crtc_mask = (1 << 2); | |
738 | clone_mask = (1 << INTEL_OUTPUT_MIPI2); | |
739 | break; | |
740 | case INTEL_OUTPUT_HDMI: | |
741 | if (IS_MFLD(dev)) | |
742 | crtc_mask = (1 << 1); | |
743 | else | |
744 | crtc_mask = (1 << 0); | |
745 | clone_mask = (1 << INTEL_OUTPUT_HDMI); | |
746 | break; | |
747 | } | |
748 | encoder->possible_crtcs = crtc_mask; | |
749 | encoder->possible_clones = | |
750 | psb_intel_connector_clones(dev, clone_mask); | |
751 | } | |
752 | } | |
753 | ||
754 | void psb_modeset_init(struct drm_device *dev) | |
755 | { | |
756 | struct drm_psb_private *dev_priv = dev->dev_private; | |
757 | struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev; | |
758 | int i; | |
759 | ||
760 | drm_mode_config_init(dev); | |
761 | ||
762 | dev->mode_config.min_width = 0; | |
763 | dev->mode_config.min_height = 0; | |
764 | ||
765 | dev->mode_config.funcs = (void *) &psb_mode_funcs; | |
766 | ||
767 | /* set memory base */ | |
768 | /* MRST and PSB should use BAR 2*/ | |
769 | pci_read_config_dword(dev->pdev, PSB_BSM, (u32 *) | |
770 | &(dev->mode_config.fb_base)); | |
771 | ||
772 | /* num pipes is 2 for PSB but 1 for Mrst */ | |
773 | for (i = 0; i < dev_priv->num_pipe; i++) | |
774 | psb_intel_crtc_init(dev, i, mode_dev); | |
775 | ||
776 | dev->mode_config.max_width = 2048; | |
777 | dev->mode_config.max_height = 2048; | |
778 | ||
779 | psb_setup_outputs(dev); | |
780 | } | |
781 | ||
782 | void psb_modeset_cleanup(struct drm_device *dev) | |
783 | { | |
784 | mutex_lock(&dev->struct_mutex); | |
785 | ||
786 | drm_kms_helper_poll_fini(dev); | |
787 | psb_fbdev_fini(dev); | |
788 | drm_mode_config_cleanup(dev); | |
789 | ||
790 | mutex_unlock(&dev->struct_mutex); | |
791 | } |