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