1 // SPDX-License-Identifier: GPL-2.0-only
2 /**************************************************************************
3  * Copyright (c) 2007-2011, Intel Corporation.
4  * All Rights Reserved.
5  *
6  **************************************************************************/
7 
8 #include <linux/console.h>
9 #include <linux/delay.h>
10 #include <linux/errno.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/mm.h>
14 #include <linux/module.h>
15 #include <linux/pfn_t.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
18 #include <linux/tty.h>
19 
20 #include <drm/drm.h>
21 #include <drm/drm_crtc.h>
22 #include <drm/drm_fb_helper.h>
23 #include <drm/drm_fourcc.h>
24 #include <drm/drm_gem_framebuffer_helper.h>
25 
26 #include "framebuffer.h"
27 #include "gem.h"
28 #include "gtt.h"
29 #include "psb_drv.h"
30 #include "psb_intel_drv.h"
31 #include "psb_intel_reg.h"
32 
33 static const struct drm_framebuffer_funcs psb_fb_funcs = {
34 	.destroy = drm_gem_fb_destroy,
35 	.create_handle = drm_gem_fb_create_handle,
36 };
37 
38 #define CMAP_TOHW(_val, _width) ((((_val) << (_width)) + 0x7FFF - (_val)) >> 16)
39 
40 static int psbfb_setcolreg(unsigned regno, unsigned red, unsigned green,
41 			   unsigned blue, unsigned transp,
42 			   struct fb_info *info)
43 {
44 	struct drm_fb_helper *fb_helper = info->par;
45 	struct drm_framebuffer *fb = fb_helper->fb;
46 	uint32_t v;
47 
48 	if (!fb)
49 		return -ENOMEM;
50 
51 	if (regno > 255)
52 		return 1;
53 
54 	red = CMAP_TOHW(red, info->var.red.length);
55 	blue = CMAP_TOHW(blue, info->var.blue.length);
56 	green = CMAP_TOHW(green, info->var.green.length);
57 	transp = CMAP_TOHW(transp, info->var.transp.length);
58 
59 	v = (red << info->var.red.offset) |
60 	    (green << info->var.green.offset) |
61 	    (blue << info->var.blue.offset) |
62 	    (transp << info->var.transp.offset);
63 
64 	if (regno < 16) {
65 		switch (fb->format->cpp[0] * 8) {
66 		case 16:
67 			((uint32_t *) info->pseudo_palette)[regno] = v;
68 			break;
69 		case 24:
70 		case 32:
71 			((uint32_t *) info->pseudo_palette)[regno] = v;
72 			break;
73 		}
74 	}
75 
76 	return 0;
77 }
78 
79 static vm_fault_t psbfb_vm_fault(struct vm_fault *vmf)
80 {
81 	struct vm_area_struct *vma = vmf->vma;
82 	struct drm_framebuffer *fb = vma->vm_private_data;
83 	struct drm_device *dev = fb->dev;
84 	struct drm_psb_private *dev_priv = dev->dev_private;
85 	struct gtt_range *gtt = to_gtt_range(fb->obj[0]);
86 	int page_num;
87 	int i;
88 	unsigned long address;
89 	vm_fault_t ret = VM_FAULT_SIGBUS;
90 	unsigned long pfn;
91 	unsigned long phys_addr = (unsigned long)dev_priv->stolen_base +
92 				  gtt->offset;
93 
94 	page_num = vma_pages(vma);
95 	address = vmf->address - (vmf->pgoff << PAGE_SHIFT);
96 
97 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
98 
99 	for (i = 0; i < page_num; i++) {
100 		pfn = (phys_addr >> PAGE_SHIFT);
101 
102 		ret = vmf_insert_mixed(vma, address,
103 				__pfn_to_pfn_t(pfn, PFN_DEV));
104 		if (unlikely(ret & VM_FAULT_ERROR))
105 			break;
106 		address += PAGE_SIZE;
107 		phys_addr += PAGE_SIZE;
108 	}
109 	return ret;
110 }
111 
112 static void psbfb_vm_open(struct vm_area_struct *vma)
113 {
114 }
115 
116 static void psbfb_vm_close(struct vm_area_struct *vma)
117 {
118 }
119 
120 static const struct vm_operations_struct psbfb_vm_ops = {
121 	.fault	= psbfb_vm_fault,
122 	.open	= psbfb_vm_open,
123 	.close	= psbfb_vm_close
124 };
125 
126 static int psbfb_mmap(struct fb_info *info, struct vm_area_struct *vma)
127 {
128 	struct drm_fb_helper *fb_helper = info->par;
129 	struct drm_framebuffer *fb = fb_helper->fb;
130 
131 	if (vma->vm_pgoff != 0)
132 		return -EINVAL;
133 	if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
134 		return -EINVAL;
135 
136 	/*
137 	 * If this is a GEM object then info->screen_base is the virtual
138 	 * kernel remapping of the object. FIXME: Review if this is
139 	 * suitable for our mmap work
140 	 */
141 	vma->vm_ops = &psbfb_vm_ops;
142 	vma->vm_private_data = (void *)fb;
143 	vma->vm_flags |= VM_IO | VM_MIXEDMAP | VM_DONTEXPAND | VM_DONTDUMP;
144 	return 0;
145 }
146 
147 static const struct fb_ops psbfb_unaccel_ops = {
148 	.owner = THIS_MODULE,
149 	DRM_FB_HELPER_DEFAULT_OPS,
150 	.fb_setcolreg = psbfb_setcolreg,
151 	.fb_fillrect = drm_fb_helper_cfb_fillrect,
152 	.fb_copyarea = drm_fb_helper_cfb_copyarea,
153 	.fb_imageblit = drm_fb_helper_cfb_imageblit,
154 	.fb_mmap = psbfb_mmap,
155 };
156 
157 /**
158  *	psb_framebuffer_init	-	initialize a framebuffer
159  *	@dev: our DRM device
160  *	@fb: framebuffer to set up
161  *	@mode_cmd: mode description
162  *	@gt: backing object
163  *
164  *	Configure and fill in the boilerplate for our frame buffer. Return
165  *	0 on success or an error code if we fail.
166  */
167 static int psb_framebuffer_init(struct drm_device *dev,
168 					struct drm_framebuffer *fb,
169 					const struct drm_mode_fb_cmd2 *mode_cmd,
170 					struct drm_gem_object *obj)
171 {
172 	const struct drm_format_info *info;
173 	int ret;
174 
175 	/*
176 	 * Reject unknown formats, YUV formats, and formats with more than
177 	 * 4 bytes per pixel.
178 	 */
179 	info = drm_get_format_info(dev, mode_cmd);
180 	if (!info || !info->depth || info->cpp[0] > 4)
181 		return -EINVAL;
182 
183 	if (mode_cmd->pitches[0] & 63)
184 		return -EINVAL;
185 
186 	drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
187 	fb->obj[0] = obj;
188 	ret = drm_framebuffer_init(dev, fb, &psb_fb_funcs);
189 	if (ret) {
190 		dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
191 		return ret;
192 	}
193 	return 0;
194 }
195 
196 /**
197  *	psb_framebuffer_create	-	create a framebuffer backed by gt
198  *	@dev: our DRM device
199  *	@mode_cmd: the description of the requested mode
200  *	@gt: the backing object
201  *
202  *	Create a framebuffer object backed by the gt, and fill in the
203  *	boilerplate required
204  *
205  *	TODO: review object references
206  */
207 
208 static struct drm_framebuffer *psb_framebuffer_create
209 			(struct drm_device *dev,
210 			 const struct drm_mode_fb_cmd2 *mode_cmd,
211 			 struct drm_gem_object *obj)
212 {
213 	struct drm_framebuffer *fb;
214 	int ret;
215 
216 	fb = kzalloc(sizeof(*fb), GFP_KERNEL);
217 	if (!fb)
218 		return ERR_PTR(-ENOMEM);
219 
220 	ret = psb_framebuffer_init(dev, fb, mode_cmd, obj);
221 	if (ret) {
222 		kfree(fb);
223 		return ERR_PTR(ret);
224 	}
225 	return fb;
226 }
227 
228 /**
229  *	psbfb_alloc		-	allocate frame buffer memory
230  *	@dev: the DRM device
231  *	@aligned_size: space needed
232  *
233  *	Allocate the frame buffer. In the usual case we get a GTT range that
234  *	is stolen memory backed and life is simple. If there isn't sufficient
235  *	we fail as we don't have the virtual mapping space to really vmap it
236  *	and the kernel console code can't handle non linear framebuffers.
237  *
238  *	Re-address this as and if the framebuffer layer grows this ability.
239  */
240 static struct gtt_range *psbfb_alloc(struct drm_device *dev, int aligned_size)
241 {
242 	struct gtt_range *backing;
243 	/* Begin by trying to use stolen memory backing */
244 	backing = psb_gtt_alloc_range(dev, aligned_size, "fb", 1, PAGE_SIZE);
245 	if (backing) {
246 		backing->gem.funcs = &psb_gem_object_funcs;
247 		drm_gem_private_object_init(dev, &backing->gem, aligned_size);
248 		return backing;
249 	}
250 	return NULL;
251 }
252 
253 /**
254  *	psbfb_create		-	create a framebuffer
255  *	@fbdev: the framebuffer device
256  *	@sizes: specification of the layout
257  *
258  *	Create a framebuffer to the specifications provided
259  */
260 static int psbfb_create(struct drm_fb_helper *fb_helper,
261 				struct drm_fb_helper_surface_size *sizes)
262 {
263 	struct drm_device *dev = fb_helper->dev;
264 	struct drm_psb_private *dev_priv = dev->dev_private;
265 	struct fb_info *info;
266 	struct drm_framebuffer *fb;
267 	struct drm_mode_fb_cmd2 mode_cmd;
268 	int size;
269 	int ret;
270 	struct gtt_range *backing;
271 	u32 bpp, depth;
272 
273 	mode_cmd.width = sizes->surface_width;
274 	mode_cmd.height = sizes->surface_height;
275 	bpp = sizes->surface_bpp;
276 	depth = sizes->surface_depth;
277 
278 	/* No 24bit packed */
279 	if (bpp == 24)
280 		bpp = 32;
281 
282 	mode_cmd.pitches[0] = ALIGN(mode_cmd.width * DIV_ROUND_UP(bpp, 8), 64);
283 
284 	size = mode_cmd.pitches[0] * mode_cmd.height;
285 	size = ALIGN(size, PAGE_SIZE);
286 
287 	/* Allocate the framebuffer in the GTT with stolen page backing */
288 	backing = psbfb_alloc(dev, size);
289 	if (backing == NULL)
290 		return -ENOMEM;
291 
292 	memset(dev_priv->vram_addr + backing->offset, 0, size);
293 
294 	info = drm_fb_helper_alloc_fbi(fb_helper);
295 	if (IS_ERR(info)) {
296 		ret = PTR_ERR(info);
297 		goto out;
298 	}
299 
300 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
301 
302 	fb = psb_framebuffer_create(dev, &mode_cmd, &backing->gem);
303 	if (IS_ERR(fb)) {
304 		ret = PTR_ERR(fb);
305 		goto out;
306 	}
307 
308 	fb_helper->fb = fb;
309 
310 	info->fbops = &psbfb_unaccel_ops;
311 
312 	info->fix.smem_start = dev->mode_config.fb_base;
313 	info->fix.smem_len = size;
314 	info->fix.ywrapstep = 0;
315 	info->fix.ypanstep = 0;
316 
317 	/* Accessed stolen memory directly */
318 	info->screen_base = dev_priv->vram_addr + backing->offset;
319 	info->screen_size = size;
320 
321 	if (dev_priv->gtt.stolen_size) {
322 		info->apertures->ranges[0].base = dev->mode_config.fb_base;
323 		info->apertures->ranges[0].size = dev_priv->gtt.stolen_size;
324 	}
325 
326 	drm_fb_helper_fill_info(info, fb_helper, sizes);
327 
328 	info->fix.mmio_start = pci_resource_start(dev->pdev, 0);
329 	info->fix.mmio_len = pci_resource_len(dev->pdev, 0);
330 
331 	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
332 
333 	dev_dbg(dev->dev, "allocated %dx%d fb\n", fb->width, fb->height);
334 
335 	return 0;
336 out:
337 	psb_gtt_free_range(dev, backing);
338 	return ret;
339 }
340 
341 /**
342  *	psb_user_framebuffer_create	-	create framebuffer
343  *	@dev: our DRM device
344  *	@filp: client file
345  *	@cmd: mode request
346  *
347  *	Create a new framebuffer backed by a userspace GEM object
348  */
349 static struct drm_framebuffer *psb_user_framebuffer_create
350 			(struct drm_device *dev, struct drm_file *filp,
351 			 const struct drm_mode_fb_cmd2 *cmd)
352 {
353 	struct drm_gem_object *obj;
354 
355 	/*
356 	 *	Find the GEM object and thus the gtt range object that is
357 	 *	to back this space
358 	 */
359 	obj = drm_gem_object_lookup(filp, cmd->handles[0]);
360 	if (obj == NULL)
361 		return ERR_PTR(-ENOENT);
362 
363 	/* Let the core code do all the work */
364 	return psb_framebuffer_create(dev, cmd, obj);
365 }
366 
367 static int psbfb_probe(struct drm_fb_helper *fb_helper,
368 				struct drm_fb_helper_surface_size *sizes)
369 {
370 	struct drm_device *dev = fb_helper->dev;
371 	struct drm_psb_private *dev_priv = dev->dev_private;
372 	unsigned int fb_size;
373 	int bytespp;
374 
375 	bytespp = sizes->surface_bpp / 8;
376 	if (bytespp == 3)	/* no 24bit packed */
377 		bytespp = 4;
378 
379 	/* If the mode will not fit in 32bit then switch to 16bit to get
380 	   a console on full resolution. The X mode setting server will
381 	   allocate its own 32bit GEM framebuffer */
382 	fb_size = ALIGN(sizes->surface_width * bytespp, 64) *
383 		  sizes->surface_height;
384 	fb_size = ALIGN(fb_size, PAGE_SIZE);
385 
386 	if (fb_size > dev_priv->vram_stolen_size) {
387                 sizes->surface_bpp = 16;
388                 sizes->surface_depth = 16;
389         }
390 
391 	return psbfb_create(fb_helper, sizes);
392 }
393 
394 static const struct drm_fb_helper_funcs psb_fb_helper_funcs = {
395 	.fb_probe = psbfb_probe,
396 };
397 
398 static int psb_fbdev_destroy(struct drm_device *dev,
399 			     struct drm_fb_helper *fb_helper)
400 {
401 	struct drm_framebuffer *fb = fb_helper->fb;
402 
403 	drm_fb_helper_unregister_fbi(fb_helper);
404 
405 	drm_fb_helper_fini(fb_helper);
406 	drm_framebuffer_unregister_private(fb);
407 	drm_framebuffer_cleanup(fb);
408 
409 	if (fb->obj[0])
410 		drm_gem_object_put(fb->obj[0]);
411 	kfree(fb);
412 
413 	return 0;
414 }
415 
416 int psb_fbdev_init(struct drm_device *dev)
417 {
418 	struct drm_fb_helper *fb_helper;
419 	struct drm_psb_private *dev_priv = dev->dev_private;
420 	int ret;
421 
422 	fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL);
423 	if (!fb_helper) {
424 		dev_err(dev->dev, "no memory\n");
425 		return -ENOMEM;
426 	}
427 
428 	dev_priv->fb_helper = fb_helper;
429 
430 	drm_fb_helper_prepare(dev, fb_helper, &psb_fb_helper_funcs);
431 
432 	ret = drm_fb_helper_init(dev, fb_helper);
433 	if (ret)
434 		goto free;
435 
436 	/* disable all the possible outputs/crtcs before entering KMS mode */
437 	drm_helper_disable_unused_functions(dev);
438 
439 	ret = drm_fb_helper_initial_config(fb_helper, 32);
440 	if (ret)
441 		goto fini;
442 
443 	return 0;
444 
445 fini:
446 	drm_fb_helper_fini(fb_helper);
447 free:
448 	kfree(fb_helper);
449 	return ret;
450 }
451 
452 static void psb_fbdev_fini(struct drm_device *dev)
453 {
454 	struct drm_psb_private *dev_priv = dev->dev_private;
455 
456 	if (!dev_priv->fb_helper)
457 		return;
458 
459 	psb_fbdev_destroy(dev, dev_priv->fb_helper);
460 	kfree(dev_priv->fb_helper);
461 	dev_priv->fb_helper = NULL;
462 }
463 
464 static const struct drm_mode_config_funcs psb_mode_funcs = {
465 	.fb_create = psb_user_framebuffer_create,
466 	.output_poll_changed = drm_fb_helper_output_poll_changed,
467 };
468 
469 static void psb_setup_outputs(struct drm_device *dev)
470 {
471 	struct drm_psb_private *dev_priv = dev->dev_private;
472 	struct drm_connector *connector;
473 
474 	drm_mode_create_scaling_mode_property(dev);
475 
476 	/* It is ok for this to fail - we just don't get backlight control */
477 	if (!dev_priv->backlight_property)
478 		dev_priv->backlight_property = drm_property_create_range(dev, 0,
479 							"backlight", 0, 100);
480 	dev_priv->ops->output_init(dev);
481 
482 	list_for_each_entry(connector, &dev->mode_config.connector_list,
483 			    head) {
484 		struct gma_encoder *gma_encoder = gma_attached_encoder(connector);
485 		struct drm_encoder *encoder = &gma_encoder->base;
486 		int crtc_mask = 0, clone_mask = 0;
487 
488 		/* valid crtcs */
489 		switch (gma_encoder->type) {
490 		case INTEL_OUTPUT_ANALOG:
491 			crtc_mask = (1 << 0);
492 			clone_mask = (1 << INTEL_OUTPUT_ANALOG);
493 			break;
494 		case INTEL_OUTPUT_SDVO:
495 			crtc_mask = dev_priv->ops->sdvo_mask;
496 			clone_mask = 0;
497 			break;
498 		case INTEL_OUTPUT_LVDS:
499 			crtc_mask = dev_priv->ops->lvds_mask;
500 			clone_mask = 0;
501 			break;
502 		case INTEL_OUTPUT_MIPI:
503 			crtc_mask = (1 << 0);
504 			clone_mask = 0;
505 			break;
506 		case INTEL_OUTPUT_MIPI2:
507 			crtc_mask = (1 << 2);
508 			clone_mask = 0;
509 			break;
510 		case INTEL_OUTPUT_HDMI:
511 			crtc_mask = dev_priv->ops->hdmi_mask;
512 			clone_mask = (1 << INTEL_OUTPUT_HDMI);
513 			break;
514 		case INTEL_OUTPUT_DISPLAYPORT:
515 			crtc_mask = (1 << 0) | (1 << 1);
516 			clone_mask = 0;
517 			break;
518 		case INTEL_OUTPUT_EDP:
519 			crtc_mask = (1 << 1);
520 			clone_mask = 0;
521 		}
522 		encoder->possible_crtcs = crtc_mask;
523 		encoder->possible_clones =
524 		    gma_connector_clones(dev, clone_mask);
525 	}
526 }
527 
528 void psb_modeset_init(struct drm_device *dev)
529 {
530 	struct drm_psb_private *dev_priv = dev->dev_private;
531 	struct psb_intel_mode_device *mode_dev = &dev_priv->mode_dev;
532 	int i;
533 
534 	drm_mode_config_init(dev);
535 
536 	dev->mode_config.min_width = 0;
537 	dev->mode_config.min_height = 0;
538 
539 	dev->mode_config.funcs = &psb_mode_funcs;
540 
541 	/* set memory base */
542 	/* Oaktrail and Poulsbo should use BAR 2*/
543 	pci_read_config_dword(dev->pdev, PSB_BSM, (u32 *)
544 					&(dev->mode_config.fb_base));
545 
546 	/* num pipes is 2 for PSB but 1 for Mrst */
547 	for (i = 0; i < dev_priv->num_pipe; i++)
548 		psb_intel_crtc_init(dev, i, mode_dev);
549 
550 	dev->mode_config.max_width = 4096;
551 	dev->mode_config.max_height = 4096;
552 
553 	psb_setup_outputs(dev);
554 
555 	if (dev_priv->ops->errata)
556 	        dev_priv->ops->errata(dev);
557 
558         dev_priv->modeset = true;
559 }
560 
561 void psb_modeset_cleanup(struct drm_device *dev)
562 {
563 	struct drm_psb_private *dev_priv = dev->dev_private;
564 	if (dev_priv->modeset) {
565 		drm_kms_helper_poll_fini(dev);
566 		psb_fbdev_fini(dev);
567 		drm_mode_config_cleanup(dev);
568 	}
569 }
570