File Index
Files and directories
Sign in
FILE

vrfb.c

File preview with syntax highlighting.

linux-6.19.14/drivers/video/fbdev/omap2/omapfb/vrfb.c C
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * VRFB Rotation Engine
  4 *
  5 * Copyright (C) 2009 Nokia Corporation
  6 * Author: Tomi Valkeinen <tomi.valkeinen@nokia.com>
  7 */
  8
  9/*#define DEBUG*/
 10
 11#include <linux/err.h>
 12#include <linux/export.h>
 13#include <linux/kernel.h>
 14#include <linux/module.h>
 15#include <linux/ioport.h>
 16#include <linux/io.h>
 17#include <linux/bitops.h>
 18#include <linux/mutex.h>
 19#include <linux/platform_device.h>
 20
 21#include <video/omapvrfb.h>
 22
 23#ifdef DEBUG
 24#define DBG(format, ...) pr_debug("VRFB: " format, ## __VA_ARGS__)
 25#else
 26#define DBG(format, ...)
 27#endif
 28
 29#define SMS_ROT_CONTROL(context)	(0x0 + 0x10 * context)
 30#define SMS_ROT_SIZE(context)		(0x4 + 0x10 * context)
 31#define SMS_ROT_PHYSICAL_BA(context)	(0x8 + 0x10 * context)
 32#define SMS_ROT_VIRT_BASE(rot)		(0x1000000 * (rot))
 33
 34#define OMAP_VRFB_SIZE			(2048 * 2048 * 4)
 35
 36#define VRFB_PAGE_WIDTH_EXP	5 /* Assuming SDRAM pagesize= 1024 */
 37#define VRFB_PAGE_HEIGHT_EXP	5 /* 1024 = 2^5 * 2^5 */
 38#define VRFB_PAGE_WIDTH		(1 << VRFB_PAGE_WIDTH_EXP)
 39#define VRFB_PAGE_HEIGHT	(1 << VRFB_PAGE_HEIGHT_EXP)
 40#define SMS_IMAGEHEIGHT_OFFSET	16
 41#define SMS_IMAGEWIDTH_OFFSET	0
 42#define SMS_PH_OFFSET		8
 43#define SMS_PW_OFFSET		4
 44#define SMS_PS_OFFSET		0
 45
 46/* bitmap of reserved contexts */
 47static unsigned long ctx_map;
 48
 49struct vrfb_ctx {
 50	u32 base;
 51	u32 physical_ba;
 52	u32 control;
 53	u32 size;
 54};
 55
 56static DEFINE_MUTEX(ctx_lock);
 57
 58/*
 59 * Access to this happens from client drivers or the PM core after wake-up.
 60 * For the first case we require locking at the driver level, for the second
 61 * we don't need locking, since no drivers will run until after the wake-up
 62 * has finished.
 63 */
 64
 65static void __iomem *vrfb_base;
 66
 67static int num_ctxs;
 68static struct vrfb_ctx *ctxs;
 69
 70static bool vrfb_loaded;
 71
 72static void omap2_sms_write_rot_control(u32 val, unsigned ctx)
 73{
 74	__raw_writel(val, vrfb_base + SMS_ROT_CONTROL(ctx));
 75}
 76
 77static void omap2_sms_write_rot_size(u32 val, unsigned ctx)
 78{
 79	__raw_writel(val, vrfb_base + SMS_ROT_SIZE(ctx));
 80}
 81
 82static void omap2_sms_write_rot_physical_ba(u32 val, unsigned ctx)
 83{
 84	__raw_writel(val, vrfb_base + SMS_ROT_PHYSICAL_BA(ctx));
 85}
 86
 87static inline void restore_hw_context(int ctx)
 88{
 89	omap2_sms_write_rot_control(ctxs[ctx].control, ctx);
 90	omap2_sms_write_rot_size(ctxs[ctx].size, ctx);
 91	omap2_sms_write_rot_physical_ba(ctxs[ctx].physical_ba, ctx);
 92}
 93
 94static u32 get_image_width_roundup(u16 width, u8 bytespp)
 95{
 96	unsigned long stride = width * bytespp;
 97	unsigned long ceil_pages_per_stride = (stride / VRFB_PAGE_WIDTH) +
 98		(stride % VRFB_PAGE_WIDTH != 0);
 99
100	return ceil_pages_per_stride * VRFB_PAGE_WIDTH / bytespp;
101}
102
103/*
104 * This the extra space needed in the VRFB physical area for VRFB to safely wrap
105 * any memory accesses to the invisible part of the virtual view to the physical
106 * area.
107 */
108static inline u32 get_extra_physical_size(u16 image_width_roundup, u8 bytespp)
109{
110	return (OMAP_VRFB_LINE_LEN - image_width_roundup) * VRFB_PAGE_HEIGHT *
111		bytespp;
112}
113
114void omap_vrfb_restore_context(void)
115{
116	int i;
117	unsigned long map = ctx_map;
118
119	for (i = ffs(map); i; i = ffs(map)) {
120		/* i=1..32 */
121		i--;
122		map &= ~(1 << i);
123		restore_hw_context(i);
124	}
125}
126
127void omap_vrfb_adjust_size(u16 *width, u16 *height,
128		u8 bytespp)
129{
130	*width = ALIGN(*width * bytespp, VRFB_PAGE_WIDTH) / bytespp;
131	*height = ALIGN(*height, VRFB_PAGE_HEIGHT);
132}
133EXPORT_SYMBOL(omap_vrfb_adjust_size);
134
135u32 omap_vrfb_min_phys_size(u16 width, u16 height, u8 bytespp)
136{
137	unsigned long image_width_roundup = get_image_width_roundup(width,
138		bytespp);
139
140	if (image_width_roundup > OMAP_VRFB_LINE_LEN)
141		return 0;
142
143	return (width * height * bytespp) + get_extra_physical_size(
144		image_width_roundup, bytespp);
145}
146EXPORT_SYMBOL(omap_vrfb_min_phys_size);
147
148u16 omap_vrfb_max_height(u32 phys_size, u16 width, u8 bytespp)
149{
150	unsigned long image_width_roundup = get_image_width_roundup(width,
151		bytespp);
152	unsigned long height;
153	unsigned long extra;
154
155	if (image_width_roundup > OMAP_VRFB_LINE_LEN)
156		return 0;
157
158	extra = get_extra_physical_size(image_width_roundup, bytespp);
159
160	if (phys_size < extra)
161		return 0;
162
163	height = (phys_size - extra) / (width * bytespp);
164
165	/* Virtual views provided by VRFB are limited to 2048x2048. */
166	return min_t(unsigned long, height, 2048);
167}
168EXPORT_SYMBOL(omap_vrfb_max_height);
169
170void omap_vrfb_setup(struct vrfb *vrfb, unsigned long paddr,
171		u16 width, u16 height,
172		unsigned bytespp, bool yuv_mode)
173{
174	unsigned pixel_size_exp;
175	u16 vrfb_width;
176	u16 vrfb_height;
177	u8 ctx = vrfb->context;
178	u32 size;
179	u32 control;
180
181	DBG("omapfb_set_vrfb(%d, %lx, %dx%d, %d, %d)\n", ctx, paddr,
182			width, height, bytespp, yuv_mode);
183
184	/* For YUV2 and UYVY modes VRFB needs to handle pixels a bit
185	 * differently. See TRM. */
186	if (yuv_mode) {
187		bytespp *= 2;
188		width /= 2;
189	}
190
191	if (bytespp == 4)
192		pixel_size_exp = 2;
193	else if (bytespp == 2)
194		pixel_size_exp = 1;
195	else {
196		BUG();
197		return;
198	}
199
200	vrfb_width = ALIGN(width * bytespp, VRFB_PAGE_WIDTH) / bytespp;
201	vrfb_height = ALIGN(height, VRFB_PAGE_HEIGHT);
202
203	DBG("vrfb w %u, h %u bytespp %d\n", vrfb_width, vrfb_height, bytespp);
204
205	size  = vrfb_width << SMS_IMAGEWIDTH_OFFSET;
206	size |= vrfb_height << SMS_IMAGEHEIGHT_OFFSET;
207
208	control  = pixel_size_exp << SMS_PS_OFFSET;
209	control |= VRFB_PAGE_WIDTH_EXP  << SMS_PW_OFFSET;
210	control |= VRFB_PAGE_HEIGHT_EXP << SMS_PH_OFFSET;
211
212	ctxs[ctx].physical_ba = paddr;
213	ctxs[ctx].size = size;
214	ctxs[ctx].control = control;
215
216	omap2_sms_write_rot_physical_ba(paddr, ctx);
217	omap2_sms_write_rot_size(size, ctx);
218	omap2_sms_write_rot_control(control, ctx);
219
220	DBG("vrfb offset pixels %d, %d\n",
221			vrfb_width - width, vrfb_height - height);
222
223	vrfb->xres = width;
224	vrfb->yres = height;
225	vrfb->xoffset = vrfb_width - width;
226	vrfb->yoffset = vrfb_height - height;
227	vrfb->bytespp = bytespp;
228	vrfb->yuv_mode = yuv_mode;
229}
230EXPORT_SYMBOL(omap_vrfb_setup);
231
232int omap_vrfb_map_angle(struct vrfb *vrfb, u16 height, u8 rot)
233{
234	unsigned long size = height * OMAP_VRFB_LINE_LEN * vrfb->bytespp;
235
236	vrfb->vaddr[rot] = ioremap_wc(vrfb->paddr[rot], size);
237
238	if (!vrfb->vaddr[rot]) {
239		printk(KERN_ERR "vrfb: ioremap failed\n");
240		return -ENOMEM;
241	}
242
243	DBG("ioremapped vrfb area %d of size %lu into %p\n", rot, size,
244		vrfb->vaddr[rot]);
245
246	return 0;
247}
248EXPORT_SYMBOL(omap_vrfb_map_angle);
249
250void omap_vrfb_release_ctx(struct vrfb *vrfb)
251{
252	int rot;
253	int ctx = vrfb->context;
254
255	if (ctx == 0xff)
256		return;
257
258	DBG("release ctx %d\n", ctx);
259
260	mutex_lock(&ctx_lock);
261
262	BUG_ON(!(ctx_map & (1 << ctx)));
263
264	clear_bit(ctx, &ctx_map);
265
266	for (rot = 0; rot < 4; ++rot) {
267		if (vrfb->paddr[rot]) {
268			release_mem_region(vrfb->paddr[rot], OMAP_VRFB_SIZE);
269			vrfb->paddr[rot] = 0;
270		}
271	}
272
273	vrfb->context = 0xff;
274
275	mutex_unlock(&ctx_lock);
276}
277EXPORT_SYMBOL(omap_vrfb_release_ctx);
278
279int omap_vrfb_request_ctx(struct vrfb *vrfb)
280{
281	int rot;
282	u32 paddr;
283	u8 ctx;
284	int r;
285
286	DBG("request ctx\n");
287
288	mutex_lock(&ctx_lock);
289
290	for (ctx = 0; ctx < num_ctxs; ++ctx)
291		if ((ctx_map & (1 << ctx)) == 0)
292			break;
293
294	if (ctx == num_ctxs) {
295		pr_err("vrfb: no free contexts\n");
296		r = -EBUSY;
297		goto out;
298	}
299
300	DBG("found free ctx %d\n", ctx);
301
302	set_bit(ctx, &ctx_map);
303
304	memset(vrfb, 0, sizeof(*vrfb));
305
306	vrfb->context = ctx;
307
308	for (rot = 0; rot < 4; ++rot) {
309		paddr = ctxs[ctx].base + SMS_ROT_VIRT_BASE(rot);
310		if (!request_mem_region(paddr, OMAP_VRFB_SIZE, "vrfb")) {
311			pr_err("vrfb: failed to reserve VRFB "
312					"area for ctx %d, rotation %d\n",
313					ctx, rot * 90);
314			omap_vrfb_release_ctx(vrfb);
315			r = -ENOMEM;
316			goto out;
317		}
318
319		vrfb->paddr[rot] = paddr;
320
321		DBG("VRFB %d/%d: %lx\n", ctx, rot*90, vrfb->paddr[rot]);
322	}
323
324	r = 0;
325out:
326	mutex_unlock(&ctx_lock);
327	return r;
328}
329EXPORT_SYMBOL(omap_vrfb_request_ctx);
330
331bool omap_vrfb_supported(void)
332{
333	return vrfb_loaded;
334}
335EXPORT_SYMBOL(omap_vrfb_supported);
336
337static int __init vrfb_probe(struct platform_device *pdev)
338{
339	struct resource *mem;
340	int i;
341
342	/* first resource is the register res, the rest are vrfb contexts */
343	vrfb_base = devm_platform_ioremap_resource(pdev, 0);
344	if (IS_ERR(vrfb_base))
345		return PTR_ERR(vrfb_base);
346
347	num_ctxs = pdev->num_resources - 1;
348
349	ctxs = devm_kcalloc(&pdev->dev,
350			num_ctxs, sizeof(struct vrfb_ctx),
351			GFP_KERNEL);
352
353	if (!ctxs)
354		return -ENOMEM;
355
356	for (i = 0; i < num_ctxs; ++i) {
357		mem = platform_get_resource(pdev, IORESOURCE_MEM, 1 + i);
358		if (!mem) {
359			dev_err(&pdev->dev, "can't get vrfb ctx %d address\n",
360					i);
361			return -EINVAL;
362		}
363
364		ctxs[i].base = mem->start;
365	}
366
367	vrfb_loaded = true;
368
369	return 0;
370}
371
372static struct platform_driver vrfb_driver = {
373	.driver.name	= "omapvrfb",
374};
375builtin_platform_driver_probe(vrfb_driver, vrfb_probe);
376
377MODULE_AUTHOR("Tomi Valkeinen <tomi.valkeinen@ti.com>");
378MODULE_DESCRIPTION("OMAP VRFB");
379MODULE_LICENSE("GPL v2");
~/ Read-only file preview.