]>
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> | |
01c8f1c4 | 24 | #include <linux/pfn_t.h> |
4d8d096e AC |
25 | #include <linux/mm.h> |
26 | #include <linux/tty.h> | |
27 | #include <linux/slab.h> | |
28 | #include <linux/delay.h> | |
4d8d096e AC |
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" | |
a6ba582d | 41 | #include "gtt.h" |
4d8d096e AC |
42 | |
43 | static void psb_user_framebuffer_destroy(struct drm_framebuffer *fb); | |
44 | static int psb_user_framebuffer_create_handle(struct drm_framebuffer *fb, | |
45 | struct drm_file *file_priv, | |
46 | unsigned int *handle); | |
47 | ||
48 | static const struct drm_framebuffer_funcs psb_fb_funcs = { | |
49 | .destroy = psb_user_framebuffer_destroy, | |
50 | .create_handle = psb_user_framebuffer_create_handle, | |
51 | }; | |
52 | ||
53 | #define CMAP_TOHW(_val, _width) ((((_val) << (_width)) + 0x7FFF - (_val)) >> 16) | |
54 | ||
55 | static int psbfb_setcolreg(unsigned regno, unsigned red, unsigned green, | |
56 | unsigned blue, unsigned transp, | |
57 | struct fb_info *info) | |
58 | { | |
59 | struct psb_fbdev *fbdev = info->par; | |
60 | struct drm_framebuffer *fb = fbdev->psb_fb_helper.fb; | |
61 | uint32_t v; | |
62 | ||
63 | if (!fb) | |
64 | return -ENOMEM; | |
65 | ||
66 | if (regno > 255) | |
67 | return 1; | |
68 | ||
69 | red = CMAP_TOHW(red, info->var.red.length); | |
70 | blue = CMAP_TOHW(blue, info->var.blue.length); | |
71 | green = CMAP_TOHW(green, info->var.green.length); | |
72 | transp = CMAP_TOHW(transp, info->var.transp.length); | |
73 | ||
74 | v = (red << info->var.red.offset) | | |
75 | (green << info->var.green.offset) | | |
76 | (blue << info->var.blue.offset) | | |
77 | (transp << info->var.transp.offset); | |
78 | ||
79 | if (regno < 16) { | |
80 | switch (fb->bits_per_pixel) { | |
81 | case 16: | |
82 | ((uint32_t *) info->pseudo_palette)[regno] = v; | |
83 | break; | |
84 | case 24: | |
85 | case 32: | |
86 | ((uint32_t *) info->pseudo_palette)[regno] = v; | |
87 | break; | |
88 | } | |
89 | } | |
90 | ||
91 | return 0; | |
92 | } | |
93 | ||
a6ba582d AC |
94 | static int psbfb_pan(struct fb_var_screeninfo *var, struct fb_info *info) |
95 | { | |
96 | struct psb_fbdev *fbdev = info->par; | |
97 | struct psb_framebuffer *psbfb = &fbdev->pfb; | |
98 | struct drm_device *dev = psbfb->base.dev; | |
99 | ||
100 | /* | |
101 | * We have to poke our nose in here. The core fb code assumes | |
102 | * panning is part of the hardware that can be invoked before | |
103 | * the actual fb is mapped. In our case that isn't quite true. | |
104 | */ | |
105 | if (psbfb->gtt->npage) { | |
106 | /* GTT roll shifts in 4K pages, we need to shift the right | |
107 | number of pages */ | |
108 | int pages = info->fix.line_length >> 12; | |
109 | psb_gtt_roll(dev, psbfb->gtt, var->yoffset * pages); | |
110 | } | |
111 | return 0; | |
112 | } | |
4d8d096e | 113 | |
4d8d096e AC |
114 | static int psbfb_vm_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
115 | { | |
116 | struct psb_framebuffer *psbfb = vma->vm_private_data; | |
117 | struct drm_device *dev = psbfb->base.dev; | |
118 | struct drm_psb_private *dev_priv = dev->dev_private; | |
119 | int page_num; | |
120 | int i; | |
121 | unsigned long address; | |
122 | int ret; | |
123 | unsigned long pfn; | |
61bb3fea PJ |
124 | unsigned long phys_addr = (unsigned long)dev_priv->stolen_base + |
125 | psbfb->gtt->offset; | |
4d8d096e | 126 | |
024b6a63 | 127 | page_num = vma_pages(vma); |
1278f7de | 128 | address = (unsigned long)vmf->virtual_address - (vmf->pgoff << PAGE_SHIFT); |
4d8d096e AC |
129 | |
130 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); | |
131 | ||
132 | for (i = 0; i < page_num; i++) { | |
133 | pfn = (phys_addr >> PAGE_SHIFT); | |
134 | ||
01c8f1c4 DW |
135 | ret = vm_insert_mixed(vma, address, |
136 | __pfn_to_pfn_t(pfn, PFN_DEV)); | |
4d8d096e AC |
137 | if (unlikely((ret == -EBUSY) || (ret != 0 && i > 0))) |
138 | break; | |
139 | else if (unlikely(ret != 0)) { | |
140 | ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS; | |
141 | return ret; | |
142 | } | |
143 | address += PAGE_SIZE; | |
144 | phys_addr += PAGE_SIZE; | |
145 | } | |
146 | return VM_FAULT_NOPAGE; | |
147 | } | |
148 | ||
149 | static void psbfb_vm_open(struct vm_area_struct *vma) | |
150 | { | |
151 | } | |
152 | ||
153 | static void psbfb_vm_close(struct vm_area_struct *vma) | |
154 | { | |
155 | } | |
156 | ||
78b68556 | 157 | static const struct vm_operations_struct psbfb_vm_ops = { |
4d8d096e AC |
158 | .fault = psbfb_vm_fault, |
159 | .open = psbfb_vm_open, | |
160 | .close = psbfb_vm_close | |
161 | }; | |
162 | ||
163 | static int psbfb_mmap(struct fb_info *info, struct vm_area_struct *vma) | |
164 | { | |
165 | struct psb_fbdev *fbdev = info->par; | |
166 | struct psb_framebuffer *psbfb = &fbdev->pfb; | |
167 | ||
168 | if (vma->vm_pgoff != 0) | |
169 | return -EINVAL; | |
170 | if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT)) | |
171 | return -EINVAL; | |
172 | ||
173 | if (!psbfb->addr_space) | |
174 | psbfb->addr_space = vma->vm_file->f_mapping; | |
175 | /* | |
176 | * If this is a GEM object then info->screen_base is the virtual | |
177 | * kernel remapping of the object. FIXME: Review if this is | |
178 | * suitable for our mmap work | |
179 | */ | |
180 | vma->vm_ops = &psbfb_vm_ops; | |
181 | vma->vm_private_data = (void *)psbfb; | |
314e51b9 | 182 | vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP; |
4d8d096e AC |
183 | return 0; |
184 | } | |
185 | ||
4d8d096e AC |
186 | static struct fb_ops psbfb_ops = { |
187 | .owner = THIS_MODULE, | |
3da6c2f3 | 188 | DRM_FB_HELPER_DEFAULT_OPS, |
4d8d096e | 189 | .fb_setcolreg = psbfb_setcolreg, |
546187c8 | 190 | .fb_fillrect = drm_fb_helper_cfb_fillrect, |
4d8d096e | 191 | .fb_copyarea = psbfb_copyarea, |
546187c8 | 192 | .fb_imageblit = drm_fb_helper_cfb_imageblit, |
4d8d096e AC |
193 | .fb_mmap = psbfb_mmap, |
194 | .fb_sync = psbfb_sync, | |
4d8d096e AC |
195 | }; |
196 | ||
a6ba582d AC |
197 | static struct fb_ops psbfb_roll_ops = { |
198 | .owner = THIS_MODULE, | |
3da6c2f3 | 199 | DRM_FB_HELPER_DEFAULT_OPS, |
a6ba582d | 200 | .fb_setcolreg = psbfb_setcolreg, |
546187c8 AT |
201 | .fb_fillrect = drm_fb_helper_cfb_fillrect, |
202 | .fb_copyarea = drm_fb_helper_cfb_copyarea, | |
203 | .fb_imageblit = drm_fb_helper_cfb_imageblit, | |
a6ba582d AC |
204 | .fb_pan_display = psbfb_pan, |
205 | .fb_mmap = psbfb_mmap, | |
a6ba582d AC |
206 | }; |
207 | ||
4d8d096e AC |
208 | static struct fb_ops psbfb_unaccel_ops = { |
209 | .owner = THIS_MODULE, | |
3da6c2f3 | 210 | DRM_FB_HELPER_DEFAULT_OPS, |
4d8d096e | 211 | .fb_setcolreg = psbfb_setcolreg, |
546187c8 AT |
212 | .fb_fillrect = drm_fb_helper_cfb_fillrect, |
213 | .fb_copyarea = drm_fb_helper_cfb_copyarea, | |
214 | .fb_imageblit = drm_fb_helper_cfb_imageblit, | |
4d8d096e | 215 | .fb_mmap = psbfb_mmap, |
4d8d096e AC |
216 | }; |
217 | ||
218 | /** | |
219 | * psb_framebuffer_init - initialize a framebuffer | |
220 | * @dev: our DRM device | |
221 | * @fb: framebuffer to set up | |
222 | * @mode_cmd: mode description | |
223 | * @gt: backing object | |
224 | * | |
225 | * Configure and fill in the boilerplate for our frame buffer. Return | |
226 | * 0 on success or an error code if we fail. | |
227 | */ | |
228 | static int psb_framebuffer_init(struct drm_device *dev, | |
229 | struct psb_framebuffer *fb, | |
1eb83451 | 230 | const struct drm_mode_fb_cmd2 *mode_cmd, |
4d8d096e AC |
231 | struct gtt_range *gt) |
232 | { | |
e0f9a4ab | 233 | const struct drm_format_info *info; |
4d8d096e AC |
234 | int ret; |
235 | ||
e0f9a4ab LP |
236 | /* |
237 | * Reject unknown formats, YUV formats, and formats with more than | |
238 | * 4 bytes per pixel. | |
239 | */ | |
240 | info = drm_format_info(mode_cmd->pixel_format); | |
241 | if (!info || !info->depth || info->cpp[0] > 4) | |
242 | return -EINVAL; | |
a9a644ac DA |
243 | |
244 | if (mode_cmd->pitches[0] & 63) | |
4d8d096e | 245 | return -EINVAL; |
e0f9a4ab | 246 | |
c7d73f6a DV |
247 | drm_helper_mode_fill_fb_struct(&fb->base, mode_cmd); |
248 | fb->gtt = gt; | |
4d8d096e AC |
249 | ret = drm_framebuffer_init(dev, &fb->base, &psb_fb_funcs); |
250 | if (ret) { | |
251 | dev_err(dev->dev, "framebuffer init failed: %d\n", ret); | |
252 | return ret; | |
253 | } | |
4d8d096e AC |
254 | return 0; |
255 | } | |
256 | ||
257 | /** | |
258 | * psb_framebuffer_create - create a framebuffer backed by gt | |
259 | * @dev: our DRM device | |
260 | * @mode_cmd: the description of the requested mode | |
261 | * @gt: the backing object | |
262 | * | |
263 | * Create a framebuffer object backed by the gt, and fill in the | |
264 | * boilerplate required | |
265 | * | |
266 | * TODO: review object references | |
267 | */ | |
268 | ||
269 | static struct drm_framebuffer *psb_framebuffer_create | |
270 | (struct drm_device *dev, | |
1eb83451 | 271 | const struct drm_mode_fb_cmd2 *mode_cmd, |
4d8d096e AC |
272 | struct gtt_range *gt) |
273 | { | |
274 | struct psb_framebuffer *fb; | |
275 | int ret; | |
276 | ||
277 | fb = kzalloc(sizeof(*fb), GFP_KERNEL); | |
278 | if (!fb) | |
279 | return ERR_PTR(-ENOMEM); | |
280 | ||
281 | ret = psb_framebuffer_init(dev, fb, mode_cmd, gt); | |
282 | if (ret) { | |
283 | kfree(fb); | |
284 | return ERR_PTR(ret); | |
285 | } | |
286 | return &fb->base; | |
287 | } | |
288 | ||
289 | /** | |
290 | * psbfb_alloc - allocate frame buffer memory | |
291 | * @dev: the DRM device | |
292 | * @aligned_size: space needed | |
293 | * | |
294 | * Allocate the frame buffer. In the usual case we get a GTT range that | |
295 | * is stolen memory backed and life is simple. If there isn't sufficient | |
dffc9ceb AC |
296 | * we fail as we don't have the virtual mapping space to really vmap it |
297 | * and the kernel console code can't handle non linear framebuffers. | |
4d8d096e | 298 | * |
dffc9ceb | 299 | * Re-address this as and if the framebuffer layer grows this ability. |
4d8d096e AC |
300 | */ |
301 | static struct gtt_range *psbfb_alloc(struct drm_device *dev, int aligned_size) | |
302 | { | |
303 | struct gtt_range *backing; | |
304 | /* Begin by trying to use stolen memory backing */ | |
c269c685 | 305 | backing = psb_gtt_alloc_range(dev, aligned_size, "fb", 1, PAGE_SIZE); |
4d8d096e | 306 | if (backing) { |
89c8233f DH |
307 | drm_gem_private_object_init(dev, &backing->gem, aligned_size); |
308 | return backing; | |
4d8d096e | 309 | } |
dffc9ceb | 310 | return NULL; |
4d8d096e AC |
311 | } |
312 | ||
313 | /** | |
314 | * psbfb_create - create a framebuffer | |
315 | * @fbdev: the framebuffer device | |
316 | * @sizes: specification of the layout | |
317 | * | |
318 | * Create a framebuffer to the specifications provided | |
319 | */ | |
320 | static int psbfb_create(struct psb_fbdev *fbdev, | |
321 | struct drm_fb_helper_surface_size *sizes) | |
322 | { | |
323 | struct drm_device *dev = fbdev->psb_fb_helper.dev; | |
324 | struct drm_psb_private *dev_priv = dev->dev_private; | |
325 | struct fb_info *info; | |
326 | struct drm_framebuffer *fb; | |
327 | struct psb_framebuffer *psbfb = &fbdev->pfb; | |
a9a644ac | 328 | struct drm_mode_fb_cmd2 mode_cmd; |
4d8d096e AC |
329 | int size; |
330 | int ret; | |
331 | struct gtt_range *backing; | |
a9a644ac | 332 | u32 bpp, depth; |
1b223c9e AC |
333 | int gtt_roll = 0; |
334 | int pitch_lines = 0; | |
4d8d096e AC |
335 | |
336 | mode_cmd.width = sizes->surface_width; | |
337 | mode_cmd.height = sizes->surface_height; | |
a9a644ac | 338 | bpp = sizes->surface_bpp; |
6aa1ead1 | 339 | depth = sizes->surface_depth; |
4d8d096e AC |
340 | |
341 | /* No 24bit packed */ | |
a9a644ac DA |
342 | if (bpp == 24) |
343 | bpp = 32; | |
4d8d096e | 344 | |
1b223c9e AC |
345 | do { |
346 | /* | |
347 | * Acceleration via the GTT requires pitch to be | |
348 | * power of two aligned. Preferably page but less | |
349 | * is ok with some fonts | |
350 | */ | |
351 | mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 7) / 8), 4096 >> pitch_lines); | |
1b223c9e AC |
352 | |
353 | size = mode_cmd.pitches[0] * mode_cmd.height; | |
354 | size = ALIGN(size, PAGE_SIZE); | |
355 | ||
356 | /* Allocate the fb in the GTT with stolen page backing */ | |
357 | backing = psbfb_alloc(dev, size); | |
358 | ||
359 | if (pitch_lines) | |
360 | pitch_lines *= 2; | |
361 | else | |
362 | pitch_lines = 1; | |
363 | gtt_roll++; | |
364 | } while (backing == NULL && pitch_lines <= 16); | |
4d8d096e | 365 | |
1b223c9e AC |
366 | /* The final pitch we accepted if we succeeded */ |
367 | pitch_lines /= 2; | |
4d8d096e | 368 | |
a6ba582d AC |
369 | if (backing == NULL) { |
370 | /* | |
371 | * We couldn't get the space we wanted, fall back to the | |
372 | * display engine requirement instead. The HW requires | |
373 | * the pitch to be 64 byte aligned | |
a6ba582d AC |
374 | */ |
375 | ||
376 | gtt_roll = 0; /* Don't use GTT accelerated scrolling */ | |
1b223c9e | 377 | pitch_lines = 64; |
a6ba582d AC |
378 | |
379 | mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 7) / 8), 64); | |
380 | ||
381 | size = mode_cmd.pitches[0] * mode_cmd.height; | |
382 | size = ALIGN(size, PAGE_SIZE); | |
383 | ||
384 | /* Allocate the framebuffer in the GTT with stolen page backing */ | |
385 | backing = psbfb_alloc(dev, size); | |
386 | if (backing == NULL) | |
387 | return -ENOMEM; | |
388 | } | |
4d8d096e | 389 | |
bb849779 AC |
390 | memset(dev_priv->vram_addr + backing->offset, 0, size); |
391 | ||
546187c8 AT |
392 | info = drm_fb_helper_alloc_fbi(&fbdev->psb_fb_helper); |
393 | if (IS_ERR(info)) { | |
394 | ret = PTR_ERR(info); | |
4cd54d98 | 395 | goto err_free_range; |
4d8d096e AC |
396 | } |
397 | info->par = fbdev; | |
398 | ||
a9a644ac DA |
399 | mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth); |
400 | ||
4d8d096e AC |
401 | ret = psb_framebuffer_init(dev, psbfb, &mode_cmd, backing); |
402 | if (ret) | |
4cd54d98 | 403 | goto err_release; |
4d8d096e AC |
404 | |
405 | fb = &psbfb->base; | |
406 | psbfb->fbdev = info; | |
407 | ||
408 | fbdev->psb_fb_helper.fb = fb; | |
4d8d096e | 409 | |
4578240b | 410 | drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth); |
f9d8149a | 411 | strcpy(info->fix.id, "psbdrmfb"); |
4d8d096e AC |
412 | |
413 | info->flags = FBINFO_DEFAULT; | |
1b223c9e AC |
414 | if (dev_priv->ops->accel_2d && pitch_lines > 8) /* 2D engine */ |
415 | info->fbops = &psbfb_ops; | |
416 | else if (gtt_roll) { /* GTT rolling seems best */ | |
a6ba582d AC |
417 | info->fbops = &psbfb_roll_ops; |
418 | info->flags |= FBINFO_HWACCEL_YPAN; | |
1b223c9e | 419 | } else /* Software */ |
a6ba582d | 420 | info->fbops = &psbfb_unaccel_ops; |
4d8d096e | 421 | |
4d8d096e AC |
422 | info->fix.smem_start = dev->mode_config.fb_base; |
423 | info->fix.smem_len = size; | |
a6ba582d AC |
424 | info->fix.ywrapstep = gtt_roll; |
425 | info->fix.ypanstep = 0; | |
4d8d096e | 426 | |
dffc9ceb | 427 | /* Accessed stolen memory directly */ |
37214ca0 | 428 | info->screen_base = dev_priv->vram_addr + backing->offset; |
4d8d096e AC |
429 | info->screen_size = size; |
430 | ||
431 | if (dev_priv->gtt.stolen_size) { | |
4d8d096e AC |
432 | info->apertures->ranges[0].base = dev->mode_config.fb_base; |
433 | info->apertures->ranges[0].size = dev_priv->gtt.stolen_size; | |
434 | } | |
435 | ||
4d8d096e AC |
436 | drm_fb_helper_fill_var(info, &fbdev->psb_fb_helper, |
437 | sizes->fb_width, sizes->fb_height); | |
438 | ||
439 | info->fix.mmio_start = pci_resource_start(dev->pdev, 0); | |
440 | info->fix.mmio_len = pci_resource_len(dev->pdev, 0); | |
441 | ||
fb2a99e1 | 442 | /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */ |
4d8d096e | 443 | |
31a0685a | 444 | dev_dbg(dev->dev, "allocated %dx%d fb\n", |
4d8d096e AC |
445 | psbfb->base.width, psbfb->base.height); |
446 | ||
4d8d096e | 447 | return 0; |
4cd54d98 | 448 | err_release: |
546187c8 | 449 | drm_fb_helper_release_fbi(&fbdev->psb_fb_helper); |
4cd54d98 | 450 | err_free_range: |
4d8d096e AC |
451 | psb_gtt_free_range(dev, backing); |
452 | return ret; | |
453 | } | |
454 | ||
455 | /** | |
456 | * psb_user_framebuffer_create - create framebuffer | |
457 | * @dev: our DRM device | |
458 | * @filp: client file | |
459 | * @cmd: mode request | |
460 | * | |
461 | * Create a new framebuffer backed by a userspace GEM object | |
462 | */ | |
463 | static struct drm_framebuffer *psb_user_framebuffer_create | |
464 | (struct drm_device *dev, struct drm_file *filp, | |
1eb83451 | 465 | const struct drm_mode_fb_cmd2 *cmd) |
4d8d096e AC |
466 | { |
467 | struct gtt_range *r; | |
468 | struct drm_gem_object *obj; | |
469 | ||
470 | /* | |
471 | * Find the GEM object and thus the gtt range object that is | |
472 | * to back this space | |
473 | */ | |
a8ad0bd8 | 474 | obj = drm_gem_object_lookup(filp, cmd->handles[0]); |
4d8d096e AC |
475 | if (obj == NULL) |
476 | return ERR_PTR(-ENOENT); | |
477 | ||
478 | /* Let the core code do all the work */ | |
479 | r = container_of(obj, struct gtt_range, gem); | |
480 | return psb_framebuffer_create(dev, cmd, r); | |
481 | } | |
482 | ||
483 | static void psbfb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green, | |
484 | u16 blue, int regno) | |
485 | { | |
6306865d | 486 | struct gma_crtc *gma_crtc = to_gma_crtc(crtc); |
3df546be | 487 | |
6306865d PJ |
488 | gma_crtc->lut_r[regno] = red >> 8; |
489 | gma_crtc->lut_g[regno] = green >> 8; | |
490 | gma_crtc->lut_b[regno] = blue >> 8; | |
4d8d096e AC |
491 | } |
492 | ||
493 | static void psbfb_gamma_get(struct drm_crtc *crtc, u16 *red, | |
494 | u16 *green, u16 *blue, int regno) | |
495 | { | |
6306865d | 496 | struct gma_crtc *gma_crtc = to_gma_crtc(crtc); |
3df546be | 497 | |
6306865d PJ |
498 | *red = gma_crtc->lut_r[regno] << 8; |
499 | *green = gma_crtc->lut_g[regno] << 8; | |
500 | *blue = gma_crtc->lut_b[regno] << 8; | |
4d8d096e AC |
501 | } |
502 | ||
503 | static int psbfb_probe(struct drm_fb_helper *helper, | |
504 | struct drm_fb_helper_surface_size *sizes) | |
505 | { | |
c39aa6a1 FF |
506 | struct psb_fbdev *psb_fbdev = |
507 | container_of(helper, struct psb_fbdev, psb_fb_helper); | |
3aad16d2 AC |
508 | struct drm_device *dev = psb_fbdev->psb_fb_helper.dev; |
509 | struct drm_psb_private *dev_priv = dev->dev_private; | |
3aad16d2 | 510 | int bytespp; |
4d8d096e | 511 | |
3aad16d2 AC |
512 | bytespp = sizes->surface_bpp / 8; |
513 | if (bytespp == 3) /* no 24bit packed */ | |
514 | bytespp = 4; | |
515 | ||
516 | /* If the mode will not fit in 32bit then switch to 16bit to get | |
517 | a console on full resolution. The X mode setting server will | |
518 | allocate its own 32bit GEM framebuffer */ | |
519 | if (ALIGN(sizes->fb_width * bytespp, 64) * sizes->fb_height > | |
520 | dev_priv->vram_stolen_size) { | |
521 | sizes->surface_bpp = 16; | |
522 | sizes->surface_depth = 16; | |
523 | } | |
524 | ||
cd5428a5 | 525 | return psbfb_create(psb_fbdev, sizes); |
4d8d096e AC |
526 | } |
527 | ||
3a493879 | 528 | static const struct drm_fb_helper_funcs psb_fb_helper_funcs = { |
4d8d096e AC |
529 | .gamma_set = psbfb_gamma_set, |
530 | .gamma_get = psbfb_gamma_get, | |
531 | .fb_probe = psbfb_probe, | |
532 | }; | |
533 | ||
bc7f2b08 | 534 | static int psb_fbdev_destroy(struct drm_device *dev, struct psb_fbdev *fbdev) |
4d8d096e | 535 | { |
4d8d096e AC |
536 | struct psb_framebuffer *psbfb = &fbdev->pfb; |
537 | ||
546187c8 AT |
538 | drm_fb_helper_unregister_fbi(&fbdev->psb_fb_helper); |
539 | drm_fb_helper_release_fbi(&fbdev->psb_fb_helper); | |
540 | ||
4d8d096e | 541 | drm_fb_helper_fini(&fbdev->psb_fb_helper); |
36206361 | 542 | drm_framebuffer_unregister_private(&psbfb->base); |
4d8d096e AC |
543 | drm_framebuffer_cleanup(&psbfb->base); |
544 | ||
545 | if (psbfb->gtt) | |
46a0f223 | 546 | drm_gem_object_unreference_unlocked(&psbfb->gtt->gem); |
4d8d096e AC |
547 | return 0; |
548 | } | |
549 | ||
550 | int psb_fbdev_init(struct drm_device *dev) | |
551 | { | |
552 | struct psb_fbdev *fbdev; | |
553 | struct drm_psb_private *dev_priv = dev->dev_private; | |
01934c2a | 554 | int ret; |
4d8d096e AC |
555 | |
556 | fbdev = kzalloc(sizeof(struct psb_fbdev), GFP_KERNEL); | |
557 | if (!fbdev) { | |
558 | dev_err(dev->dev, "no memory\n"); | |
559 | return -ENOMEM; | |
560 | } | |
561 | ||
562 | dev_priv->fbdev = fbdev; | |
10a23102 TR |
563 | |
564 | drm_fb_helper_prepare(dev, &fbdev->psb_fb_helper, &psb_fb_helper_funcs); | |
4d8d096e | 565 | |
01934c2a TR |
566 | ret = drm_fb_helper_init(dev, &fbdev->psb_fb_helper, |
567 | dev_priv->ops->crtcs, INTELFB_CONN_LIMIT); | |
568 | if (ret) | |
569 | goto free; | |
4d8d096e | 570 | |
01934c2a TR |
571 | ret = drm_fb_helper_single_add_all_connectors(&fbdev->psb_fb_helper); |
572 | if (ret) | |
573 | goto fini; | |
76a39dbf DV |
574 | |
575 | /* disable all the possible outputs/crtcs before entering KMS mode */ | |
576 | drm_helper_disable_unused_functions(dev); | |
577 | ||
01934c2a TR |
578 | ret = drm_fb_helper_initial_config(&fbdev->psb_fb_helper, 32); |
579 | if (ret) | |
580 | goto fini; | |
581 | ||
4d8d096e | 582 | return 0; |
01934c2a TR |
583 | |
584 | fini: | |
585 | drm_fb_helper_fini(&fbdev->psb_fb_helper); | |
586 | free: | |
587 | kfree(fbdev); | |
588 | return ret; | |
4d8d096e AC |
589 | } |
590 | ||
bc7f2b08 | 591 | static void psb_fbdev_fini(struct drm_device *dev) |
4d8d096e AC |
592 | { |
593 | struct drm_psb_private *dev_priv = dev->dev_private; | |
594 | ||
595 | if (!dev_priv->fbdev) | |
596 | return; | |
597 | ||
598 | psb_fbdev_destroy(dev, dev_priv->fbdev); | |
599 | kfree(dev_priv->fbdev); | |
600 | dev_priv->fbdev = NULL; | |
601 | } | |
602 | ||
603 | static void psbfb_output_poll_changed(struct drm_device *dev) | |
604 | { | |
605 | struct drm_psb_private *dev_priv = dev->dev_private; | |
606 | struct psb_fbdev *fbdev = (struct psb_fbdev *)dev_priv->fbdev; | |
607 | drm_fb_helper_hotplug_event(&fbdev->psb_fb_helper); | |
608 | } | |
609 | ||
610 | /** | |
611 | * psb_user_framebuffer_create_handle - add hamdle to a framebuffer | |
612 | * @fb: framebuffer | |
613 | * @file_priv: our DRM file | |
614 | * @handle: returned handle | |
615 | * | |
616 | * Our framebuffer object is a GTT range which also contains a GEM | |
617 | * object. We need to turn it into a handle for userspace. GEM will do | |
618 | * the work for us | |
619 | */ | |
620 | static int psb_user_framebuffer_create_handle(struct drm_framebuffer *fb, | |
621 | struct drm_file *file_priv, | |
622 | unsigned int *handle) | |
623 | { | |
624 | struct psb_framebuffer *psbfb = to_psb_fb(fb); | |
625 | struct gtt_range *r = psbfb->gtt; | |
626 | return drm_gem_handle_create(file_priv, &r->gem, handle); | |
627 | } | |
628 | ||
629 | /** | |
630 | * psb_user_framebuffer_destroy - destruct user created fb | |
631 | * @fb: framebuffer | |
632 | * | |
633 | * User framebuffers are backed by GEM objects so all we have to do is | |
634 | * clean up a bit and drop the reference, GEM will handle the fallout | |
635 | */ | |
636 | static void psb_user_framebuffer_destroy(struct drm_framebuffer *fb) | |
637 | { | |
638 | struct psb_framebuffer *psbfb = to_psb_fb(fb); | |
639 | struct gtt_range *r = psbfb->gtt; | |
4d8d096e AC |
640 | |
641 | /* Let DRM do its clean up */ | |
642 | drm_framebuffer_cleanup(fb); | |
643 | /* We are no longer using the resource in GEM */ | |
644 | drm_gem_object_unreference_unlocked(&r->gem); | |
645 | kfree(fb); | |
646 | } | |
647 | ||
648 | static const struct drm_mode_config_funcs psb_mode_funcs = { | |
649 | .fb_create = psb_user_framebuffer_create, | |
650 | .output_poll_changed = psbfb_output_poll_changed, | |
651 | }; | |
652 | ||
4d8d096e AC |
653 | static void psb_setup_outputs(struct drm_device *dev) |
654 | { | |
655 | struct drm_psb_private *dev_priv = dev->dev_private; | |
656 | struct drm_connector *connector; | |
657 | ||
658 | drm_mode_create_scaling_mode_property(dev); | |
4d8d096e | 659 | |
13619ce5 AC |
660 | /* It is ok for this to fail - we just don't get backlight control */ |
661 | if (!dev_priv->backlight_property) | |
662 | dev_priv->backlight_property = drm_property_create_range(dev, 0, | |
663 | "backlight", 0, 100); | |
4d8d096e AC |
664 | dev_priv->ops->output_init(dev); |
665 | ||
666 | list_for_each_entry(connector, &dev->mode_config.connector_list, | |
667 | head) { | |
367e4408 PJ |
668 | struct gma_encoder *gma_encoder = gma_attached_encoder(connector); |
669 | struct drm_encoder *encoder = &gma_encoder->base; | |
4d8d096e AC |
670 | int crtc_mask = 0, clone_mask = 0; |
671 | ||
672 | /* valid crtcs */ | |
367e4408 | 673 | switch (gma_encoder->type) { |
4d8d096e AC |
674 | case INTEL_OUTPUT_ANALOG: |
675 | crtc_mask = (1 << 0); | |
676 | clone_mask = (1 << INTEL_OUTPUT_ANALOG); | |
677 | break; | |
678 | case INTEL_OUTPUT_SDVO: | |
cf8efd3a | 679 | crtc_mask = dev_priv->ops->sdvo_mask; |
4d8d096e AC |
680 | clone_mask = (1 << INTEL_OUTPUT_SDVO); |
681 | break; | |
682 | case INTEL_OUTPUT_LVDS: | |
d235e64a | 683 | crtc_mask = dev_priv->ops->lvds_mask; |
4d8d096e AC |
684 | clone_mask = (1 << INTEL_OUTPUT_LVDS); |
685 | break; | |
686 | case INTEL_OUTPUT_MIPI: | |
687 | crtc_mask = (1 << 0); | |
688 | clone_mask = (1 << INTEL_OUTPUT_MIPI); | |
689 | break; | |
690 | case INTEL_OUTPUT_MIPI2: | |
691 | crtc_mask = (1 << 2); | |
692 | clone_mask = (1 << INTEL_OUTPUT_MIPI2); | |
693 | break; | |
694 | case INTEL_OUTPUT_HDMI: | |
d235e64a | 695 | crtc_mask = dev_priv->ops->hdmi_mask; |
4d8d096e AC |
696 | clone_mask = (1 << INTEL_OUTPUT_HDMI); |
697 | break; | |
220801bd AC |
698 | case INTEL_OUTPUT_DISPLAYPORT: |
699 | crtc_mask = (1 << 0) | (1 << 1); | |
700 | clone_mask = (1 << INTEL_OUTPUT_DISPLAYPORT); | |
701 | break; | |
d112a816 ZY |
702 | case INTEL_OUTPUT_EDP: |
703 | crtc_mask = (1 << 1); | |
704 | clone_mask = (1 << INTEL_OUTPUT_EDP); | |
4d8d096e AC |
705 | } |
706 | encoder->possible_crtcs = crtc_mask; | |
707 | encoder->possible_clones = | |
a3d5d75f | 708 | gma_connector_clones(dev, clone_mask); |
4d8d096e AC |
709 | } |
710 | } | |
711 | ||
712 | void psb_modeset_init(struct drm_device *dev) | |
713 | { | |
714 | struct drm_psb_private *dev_priv = dev->dev_private; | |
715 | struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev; | |
716 | int i; | |
717 | ||
718 | drm_mode_config_init(dev); | |
719 | ||
720 | dev->mode_config.min_width = 0; | |
721 | dev->mode_config.min_height = 0; | |
722 | ||
e6ecefaa | 723 | dev->mode_config.funcs = &psb_mode_funcs; |
4d8d096e AC |
724 | |
725 | /* set memory base */ | |
dffc9ceb | 726 | /* Oaktrail and Poulsbo should use BAR 2*/ |
4d8d096e AC |
727 | pci_read_config_dword(dev->pdev, PSB_BSM, (u32 *) |
728 | &(dev->mode_config.fb_base)); | |
729 | ||
730 | /* num pipes is 2 for PSB but 1 for Mrst */ | |
731 | for (i = 0; i < dev_priv->num_pipe; i++) | |
732 | psb_intel_crtc_init(dev, i, mode_dev); | |
733 | ||
cbbd379a PJ |
734 | dev->mode_config.max_width = 4096; |
735 | dev->mode_config.max_height = 4096; | |
4d8d096e AC |
736 | |
737 | psb_setup_outputs(dev); | |
d235e64a AC |
738 | |
739 | if (dev_priv->ops->errata) | |
740 | dev_priv->ops->errata(dev); | |
4ab2c7f1 AC |
741 | |
742 | dev_priv->modeset = true; | |
4d8d096e AC |
743 | } |
744 | ||
745 | void psb_modeset_cleanup(struct drm_device *dev) | |
746 | { | |
4ab2c7f1 AC |
747 | struct drm_psb_private *dev_priv = dev->dev_private; |
748 | if (dev_priv->modeset) { | |
4ab2c7f1 AC |
749 | drm_kms_helper_poll_fini(dev); |
750 | psb_fbdev_fini(dev); | |
751 | drm_mode_config_cleanup(dev); | |
4ab2c7f1 | 752 | } |
4d8d096e | 753 | } |