1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2015 Google, Inc
4  */
5 
6 #include <common.h>
7 #include <console.h>
8 #include <cpu_func.h>
9 #include <dm.h>
10 #include <log.h>
11 #include <malloc.h>
12 #include <mapmem.h>
13 #include <stdio_dev.h>
14 #include <video.h>
15 #include <video_console.h>
16 #include <asm/cache.h>
17 #include <asm/global_data.h>
18 #include <dm/lists.h>
19 #include <dm/device_compat.h>
20 #include <dm/device-internal.h>
21 #include <dm/uclass-internal.h>
22 #ifdef CONFIG_SANDBOX
23 #include <asm/sdl.h>
24 #endif
25 
26 /*
27  * Theory of operation:
28  *
29  * Before relocation each device is bound. The driver for each device must
30  * set the @align and @size values in struct video_uc_plat. This
31  * information represents the requires size and alignment of the frame buffer
32  * for the device. The values can be an over-estimate but cannot be too
33  * small. The actual values will be suppled (in the same manner) by the bind()
34  * method after relocation.
35  *
36  * This information is then picked up by video_reserve() which works out how
37  * much memory is needed for all devices. This is allocated between
38  * gd->video_bottom and gd->video_top.
39  *
40  * After relocation the same process occurs. The driver supplies the same
41  * @size and @align information and this time video_post_bind() checks that
42  * the drivers does not overflow the allocated memory.
43  *
44  * The frame buffer address is actually set (to plat->base) in
45  * video_post_probe(). This function also clears the frame buffer and
46  * allocates a suitable text console device. This can then be used to write
47  * text to the video device.
48  */
49 DECLARE_GLOBAL_DATA_PTR;
50 
51 /**
52  * struct video_uc_priv - Information for the video uclass
53  *
54  * @video_ptr: Current allocation position of the video framebuffer pointer.
55  *	While binding devices after relocation, this points to the next
56  *	available address to use for a device's framebuffer. It starts at
57  *	gd->video_top and works downwards, running out of space when it hits
58  *	gd->video_bottom.
59  */
60 struct video_uc_priv {
61 	ulong video_ptr;
62 };
63 
video_set_flush_dcache(struct udevice * dev,bool flush)64 void video_set_flush_dcache(struct udevice *dev, bool flush)
65 {
66 	struct video_priv *priv = dev_get_uclass_priv(dev);
67 
68 	priv->flush_dcache = flush;
69 }
70 
alloc_fb(struct udevice * dev,ulong * addrp)71 static ulong alloc_fb(struct udevice *dev, ulong *addrp)
72 {
73 	struct video_uc_plat *plat = dev_get_uclass_plat(dev);
74 	ulong base, align, size;
75 
76 	if (!plat->size)
77 		return 0;
78 
79 	align = plat->align ? plat->align : 1 << 20;
80 	base = *addrp - plat->size;
81 	base &= ~(align - 1);
82 	plat->base = base;
83 	size = *addrp - base;
84 	*addrp = base;
85 
86 	return size;
87 }
88 
video_reserve(ulong * addrp)89 int video_reserve(ulong *addrp)
90 {
91 	struct udevice *dev;
92 	ulong size;
93 
94 	gd->video_top = *addrp;
95 	for (uclass_find_first_device(UCLASS_VIDEO, &dev);
96 	     dev;
97 	     uclass_find_next_device(&dev)) {
98 		size = alloc_fb(dev, addrp);
99 		debug("%s: Reserving %lx bytes at %lx for video device '%s'\n",
100 		      __func__, size, *addrp, dev->name);
101 	}
102 
103 	/* Allocate space for PCI video devices in case there were not bound */
104 	if (*addrp == gd->video_top)
105 		*addrp -= CONFIG_VIDEO_PCI_DEFAULT_FB_SIZE;
106 
107 	gd->video_bottom = *addrp;
108 	gd->fb_base = *addrp;
109 	debug("Video frame buffers from %lx to %lx\n", gd->video_bottom,
110 	      gd->video_top);
111 
112 	return 0;
113 }
114 
video_clear(struct udevice * dev)115 int video_clear(struct udevice *dev)
116 {
117 	struct video_priv *priv = dev_get_uclass_priv(dev);
118 	int ret;
119 
120 	switch (priv->bpix) {
121 	case VIDEO_BPP16:
122 		if (IS_ENABLED(CONFIG_VIDEO_BPP16)) {
123 			u16 *ppix = priv->fb;
124 			u16 *end = priv->fb + priv->fb_size;
125 
126 			while (ppix < end)
127 				*ppix++ = priv->colour_bg;
128 			break;
129 		}
130 	case VIDEO_BPP32:
131 		if (IS_ENABLED(CONFIG_VIDEO_BPP32)) {
132 			u32 *ppix = priv->fb;
133 			u32 *end = priv->fb + priv->fb_size;
134 
135 			while (ppix < end)
136 				*ppix++ = priv->colour_bg;
137 			break;
138 		}
139 	default:
140 		memset(priv->fb, priv->colour_bg, priv->fb_size);
141 		break;
142 	}
143 	ret = video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
144 	if (ret)
145 		return ret;
146 
147 	return video_sync(dev, false);
148 }
149 
video_set_default_colors(struct udevice * dev,bool invert)150 void video_set_default_colors(struct udevice *dev, bool invert)
151 {
152 	struct video_priv *priv = dev_get_uclass_priv(dev);
153 	int fore, back;
154 
155 	if (CONFIG_IS_ENABLED(SYS_WHITE_ON_BLACK)) {
156 		/* White is used when switching to bold, use light gray here */
157 		fore = VID_LIGHT_GRAY;
158 		back = VID_BLACK;
159 	} else {
160 		fore = VID_BLACK;
161 		back = VID_WHITE;
162 	}
163 	if (invert) {
164 		int temp;
165 
166 		temp = fore;
167 		fore = back;
168 		back = temp;
169 	}
170 	priv->fg_col_idx = fore;
171 	priv->bg_col_idx = back;
172 	priv->colour_fg = vid_console_color(priv, fore);
173 	priv->colour_bg = vid_console_color(priv, back);
174 }
175 
176 /* Flush video activity to the caches */
video_sync(struct udevice * vid,bool force)177 int video_sync(struct udevice *vid, bool force)
178 {
179 	struct video_ops *ops = video_get_ops(vid);
180 	int ret;
181 
182 	if (ops && ops->video_sync) {
183 		ret = ops->video_sync(vid);
184 		if (ret)
185 			return ret;
186 	}
187 
188 	/*
189 	 * flush_dcache_range() is declared in common.h but it seems that some
190 	 * architectures do not actually implement it. Is there a way to find
191 	 * out whether it exists? For now, ARM is safe.
192 	 */
193 #if defined(CONFIG_ARM) && !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
194 	struct video_priv *priv = dev_get_uclass_priv(vid);
195 
196 	if (priv->flush_dcache) {
197 		flush_dcache_range((ulong)priv->fb,
198 				   ALIGN((ulong)priv->fb + priv->fb_size,
199 					 CONFIG_SYS_CACHELINE_SIZE));
200 	}
201 #elif defined(CONFIG_VIDEO_SANDBOX_SDL)
202 	struct video_priv *priv = dev_get_uclass_priv(vid);
203 	static ulong last_sync;
204 
205 	if (force || get_timer(last_sync) > 10) {
206 		sandbox_sdl_sync(priv->fb);
207 		last_sync = get_timer(0);
208 	}
209 #endif
210 	return 0;
211 }
212 
video_sync_all(void)213 void video_sync_all(void)
214 {
215 	struct udevice *dev;
216 	int ret;
217 
218 	for (uclass_find_first_device(UCLASS_VIDEO, &dev);
219 	     dev;
220 	     uclass_find_next_device(&dev)) {
221 		if (device_active(dev)) {
222 			ret = video_sync(dev, true);
223 			if (ret)
224 				dev_dbg(dev, "Video sync failed\n");
225 		}
226 	}
227 }
228 
video_get_xsize(struct udevice * dev)229 int video_get_xsize(struct udevice *dev)
230 {
231 	struct video_priv *priv = dev_get_uclass_priv(dev);
232 
233 	return priv->xsize;
234 }
235 
video_get_ysize(struct udevice * dev)236 int video_get_ysize(struct udevice *dev)
237 {
238 	struct video_priv *priv = dev_get_uclass_priv(dev);
239 
240 	return priv->ysize;
241 }
242 
243 #ifdef CONFIG_VIDEO_COPY
video_sync_copy(struct udevice * dev,void * from,void * to)244 int video_sync_copy(struct udevice *dev, void *from, void *to)
245 {
246 	struct video_priv *priv = dev_get_uclass_priv(dev);
247 
248 	if (priv->copy_fb) {
249 		long offset, size;
250 
251 		/* Find the offset of the first byte to copy */
252 		if ((ulong)to > (ulong)from) {
253 			size = to - from;
254 			offset = from - priv->fb;
255 		} else {
256 			size = from - to;
257 			offset = to - priv->fb;
258 		}
259 
260 		/*
261 		 * Allow a bit of leeway for valid requests somewhere near the
262 		 * frame buffer
263 		 */
264 		if (offset < -priv->fb_size || offset > 2 * priv->fb_size) {
265 #ifdef DEBUG
266 			char str[80];
267 
268 			snprintf(str, sizeof(str),
269 				 "[sync_copy fb=%p, from=%p, to=%p, offset=%lx]",
270 				 priv->fb, from, to, offset);
271 			console_puts_select_stderr(true, str);
272 #endif
273 			return -EFAULT;
274 		}
275 
276 		/*
277 		 * Silently crop the memcpy. This allows callers to avoid doing
278 		 * this themselves. It is common for the end pointer to go a
279 		 * few lines after the end of the frame buffer, since most of
280 		 * the update algorithms terminate a line after their last write
281 		 */
282 		if (offset + size > priv->fb_size) {
283 			size = priv->fb_size - offset;
284 		} else if (offset < 0) {
285 			size += offset;
286 			offset = 0;
287 		}
288 
289 		memcpy(priv->copy_fb + offset, priv->fb + offset, size);
290 	}
291 
292 	return 0;
293 }
294 
video_sync_copy_all(struct udevice * dev)295 int video_sync_copy_all(struct udevice *dev)
296 {
297 	struct video_priv *priv = dev_get_uclass_priv(dev);
298 
299 	video_sync_copy(dev, priv->fb, priv->fb + priv->fb_size);
300 
301 	return 0;
302 }
303 
304 #endif
305 
306 /* Set up the colour map */
video_pre_probe(struct udevice * dev)307 static int video_pre_probe(struct udevice *dev)
308 {
309 	struct video_priv *priv = dev_get_uclass_priv(dev);
310 
311 	priv->cmap = calloc(256, sizeof(ushort));
312 	if (!priv->cmap)
313 		return -ENOMEM;
314 
315 	return 0;
316 }
317 
video_pre_remove(struct udevice * dev)318 static int video_pre_remove(struct udevice *dev)
319 {
320 	struct video_priv *priv = dev_get_uclass_priv(dev);
321 
322 	free(priv->cmap);
323 
324 	return 0;
325 }
326 
327 /* Set up the display ready for use */
video_post_probe(struct udevice * dev)328 static int video_post_probe(struct udevice *dev)
329 {
330 	struct video_uc_plat *plat = dev_get_uclass_plat(dev);
331 	struct video_priv *priv = dev_get_uclass_priv(dev);
332 	char name[30], drv[15], *str;
333 	const char *drv_name = drv;
334 	struct udevice *cons;
335 	int ret;
336 
337 	/* Set up the line and display size */
338 	priv->fb = map_sysmem(plat->base, plat->size);
339 	if (!priv->line_length)
340 		priv->line_length = priv->xsize * VNBYTES(priv->bpix);
341 
342 	priv->fb_size = priv->line_length * priv->ysize;
343 
344 	if (IS_ENABLED(CONFIG_VIDEO_COPY) && plat->copy_base)
345 		priv->copy_fb = map_sysmem(plat->copy_base, plat->size);
346 
347 	/* Set up colors  */
348 	video_set_default_colors(dev, false);
349 
350 	if (!CONFIG_IS_ENABLED(NO_FB_CLEAR))
351 		video_clear(dev);
352 
353 	/*
354 	 * Create a text console device. For now we always do this, although
355 	 * it might be useful to support only bitmap drawing on the device
356 	 * for boards that don't need to display text. We create a TrueType
357 	 * console if enabled, a rotated console if the video driver requests
358 	 * it, otherwise a normal console.
359 	 *
360 	 * The console can be override by setting vidconsole_drv_name before
361 	 * probing this video driver, or in the probe() method.
362 	 *
363 	 * TrueType does not support rotation at present so fall back to the
364 	 * rotated console in that case.
365 	 */
366 	if (!priv->rot && IS_ENABLED(CONFIG_CONSOLE_TRUETYPE)) {
367 		snprintf(name, sizeof(name), "%s.vidconsole_tt", dev->name);
368 		strcpy(drv, "vidconsole_tt");
369 	} else {
370 		snprintf(name, sizeof(name), "%s.vidconsole%d", dev->name,
371 			 priv->rot);
372 		snprintf(drv, sizeof(drv), "vidconsole%d", priv->rot);
373 	}
374 
375 	str = strdup(name);
376 	if (!str)
377 		return -ENOMEM;
378 	if (priv->vidconsole_drv_name)
379 		drv_name = priv->vidconsole_drv_name;
380 	ret = device_bind_driver(dev, drv_name, str, &cons);
381 	if (ret) {
382 		debug("%s: Cannot bind console driver\n", __func__);
383 		return ret;
384 	}
385 
386 	ret = device_probe(cons);
387 	if (ret) {
388 		debug("%s: Cannot probe console driver\n", __func__);
389 		return ret;
390 	}
391 
392 	return 0;
393 };
394 
395 /* Post-relocation, allocate memory for the frame buffer */
video_post_bind(struct udevice * dev)396 static int video_post_bind(struct udevice *dev)
397 {
398 	struct video_uc_priv *uc_priv;
399 	ulong addr;
400 	ulong size;
401 
402 	/* Before relocation there is nothing to do here */
403 	if (!(gd->flags & GD_FLG_RELOC))
404 		return 0;
405 
406 	/* Set up the video pointer, if this is the first device */
407 	uc_priv = uclass_get_priv(dev->uclass);
408 	if (!uc_priv->video_ptr)
409 		uc_priv->video_ptr = gd->video_top;
410 
411 	/* Allocate framebuffer space for this device */
412 	addr = uc_priv->video_ptr;
413 	size = alloc_fb(dev, &addr);
414 	if (addr < gd->video_bottom) {
415 		/* Device tree node may need the 'u-boot,dm-pre-reloc' or
416 		 * 'u-boot,dm-pre-proper' tag
417 		 */
418 		printf("Video device '%s' cannot allocate frame buffer memory -ensure the device is set up before relocation\n",
419 		       dev->name);
420 		return -ENOSPC;
421 	}
422 	debug("%s: Claiming %lx bytes at %lx for video device '%s'\n",
423 	      __func__, size, addr, dev->name);
424 	uc_priv->video_ptr = addr;
425 
426 	return 0;
427 }
428 
429 UCLASS_DRIVER(video) = {
430 	.id		= UCLASS_VIDEO,
431 	.name		= "video",
432 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
433 	.post_bind	= video_post_bind,
434 	.pre_probe	= video_pre_probe,
435 	.post_probe	= video_post_probe,
436 	.pre_remove	= video_pre_remove,
437 	.priv_auto	= sizeof(struct video_uc_priv),
438 	.per_device_auto	= sizeof(struct video_priv),
439 	.per_device_plat_auto	= sizeof(struct video_uc_plat),
440 };
441