Linux Audio

Check our new training course

Loading...
v4.17
  1/*
  2 * Copyright (C) 2012 Red Hat
  3 *
  4 * based in parts on udlfb.c:
  5 * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
  6 * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
  7 * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
  8 *
  9 * This file is subject to the terms and conditions of the GNU General Public
 10 * License v2. See the file COPYING in the main directory of this archive for
 11 * more details.
 12 */
 13#include <linux/module.h>
 14#include <linux/slab.h>
 15#include <linux/fb.h>
 16#include <linux/dma-buf.h>
 17#include <linux/mem_encrypt.h>
 18
 19#include <drm/drmP.h>
 20#include <drm/drm_crtc.h>
 21#include <drm/drm_crtc_helper.h>
 
 22#include "udl_drv.h"
 23
 24#include <drm/drm_fb_helper.h>
 25
 26#define DL_DEFIO_WRITE_DELAY    (HZ/20) /* fb_deferred_io.delay in jiffies */
 27
 28static int fb_defio = 0;  /* Optionally enable experimental fb_defio mmap support */
 29static int fb_bpp = 16;
 30
 31module_param(fb_bpp, int, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
 32module_param(fb_defio, int, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
 33
 34struct udl_fbdev {
 35	struct drm_fb_helper helper;
 36	struct udl_framebuffer ufb;
 
 37	int fb_count;
 38};
 39
 40#define DL_ALIGN_UP(x, a) ALIGN(x, a)
 41#define DL_ALIGN_DOWN(x, a) ALIGN_DOWN(x, a)
 42
 43/** Read the red component (0..255) of a 32 bpp colour. */
 44#define DLO_RGB_GETRED(col) (uint8_t)((col) & 0xFF)
 45
 46/** Read the green component (0..255) of a 32 bpp colour. */
 47#define DLO_RGB_GETGRN(col) (uint8_t)(((col) >> 8) & 0xFF)
 48
 49/** Read the blue component (0..255) of a 32 bpp colour. */
 50#define DLO_RGB_GETBLU(col) (uint8_t)(((col) >> 16) & 0xFF)
 51
 52/** Return red/green component of a 16 bpp colour number. */
 53#define DLO_RG16(red, grn) (uint8_t)((((red) & 0xF8) | ((grn) >> 5)) & 0xFF)
 54
 55/** Return green/blue component of a 16 bpp colour number. */
 56#define DLO_GB16(grn, blu) (uint8_t)(((((grn) & 0x1C) << 3) | ((blu) >> 3)) & 0xFF)
 57
 58/** Return 8 bpp colour number from red, green and blue components. */
 59#define DLO_RGB8(red, grn, blu) ((((red) << 5) | (((grn) & 3) << 3) | ((blu) & 7)) & 0xFF)
 60
 61#if 0
 62static uint8_t rgb8(uint32_t col)
 63{
 64	uint8_t red = DLO_RGB_GETRED(col);
 65	uint8_t grn = DLO_RGB_GETGRN(col);
 66	uint8_t blu = DLO_RGB_GETBLU(col);
 67
 68	return DLO_RGB8(red, grn, blu);
 69}
 70
 71static uint16_t rgb16(uint32_t col)
 72{
 73	uint8_t red = DLO_RGB_GETRED(col);
 74	uint8_t grn = DLO_RGB_GETGRN(col);
 75	uint8_t blu = DLO_RGB_GETBLU(col);
 76
 77	return (DLO_RG16(red, grn) << 8) + DLO_GB16(grn, blu);
 78}
 79#endif
 80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 81int udl_handle_damage(struct udl_framebuffer *fb, int x, int y,
 82		      int width, int height)
 83{
 84	struct drm_device *dev = fb->base.dev;
 85	struct udl_device *udl = dev->dev_private;
 86	int i, ret;
 87	char *cmd;
 88	cycles_t start_cycles, end_cycles;
 89	int bytes_sent = 0;
 90	int bytes_identical = 0;
 91	struct urb *urb;
 92	int aligned_x;
 93	int bpp = fb->base.format->cpp[0];
 94
 95	if (!fb->active_16)
 96		return 0;
 97
 98	if (!fb->obj->vmapping) {
 99		ret = udl_gem_vmap(fb->obj);
100		if (ret == -ENOMEM) {
101			DRM_ERROR("failed to vmap fb\n");
102			return 0;
103		}
104		if (!fb->obj->vmapping) {
105			DRM_ERROR("failed to vmapping\n");
106			return 0;
107		}
108	}
109
 
 
110	aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
111	width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
112	x = aligned_x;
113
114	if ((width <= 0) ||
115	    (x + width > fb->base.width) ||
116	    (y + height > fb->base.height))
117		return -EINVAL;
118
119	start_cycles = get_cycles();
120
121	urb = udl_get_urb(dev);
122	if (!urb)
123		return 0;
124	cmd = urb->transfer_buffer;
125
126	for (i = y; i < y + height ; i++) {
127		const int line_offset = fb->base.pitches[0] * i;
128		const int byte_offset = line_offset + (x * bpp);
129		const int dev_byte_offset = (fb->base.width * bpp * i) + (x * bpp);
130		if (udl_render_hline(dev, bpp, &urb,
131				     (char *) fb->obj->vmapping,
132				     &cmd, byte_offset, dev_byte_offset,
133				     width * bpp,
134				     &bytes_identical, &bytes_sent))
135			goto error;
136	}
137
138	if (cmd > (char *) urb->transfer_buffer) {
139		/* Send partial buffer remaining before exiting */
140		int len = cmd - (char *) urb->transfer_buffer;
141		ret = udl_submit_urb(dev, urb, len);
142		bytes_sent += len;
143	} else
144		udl_urb_completion(urb);
145
146error:
147	atomic_add(bytes_sent, &udl->bytes_sent);
148	atomic_add(bytes_identical, &udl->bytes_identical);
149	atomic_add(width*height*bpp, &udl->bytes_rendered);
150	end_cycles = get_cycles();
151	atomic_add(((unsigned int) ((end_cycles - start_cycles)
152		    >> 10)), /* Kcycles */
153		   &udl->cpu_kcycles_used);
154
155	return 0;
156}
157
158static int udl_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
159{
160	unsigned long start = vma->vm_start;
161	unsigned long size = vma->vm_end - vma->vm_start;
162	unsigned long offset;
163	unsigned long page, pos;
164
165	if (vma->vm_pgoff > (~0UL >> PAGE_SHIFT))
166		return -EINVAL;
167
168	offset = vma->vm_pgoff << PAGE_SHIFT;
169
170	if (offset > info->fix.smem_len || size > info->fix.smem_len - offset)
171		return -EINVAL;
172
173	pos = (unsigned long)info->fix.smem_start + offset;
174
175	pr_notice("mmap() framebuffer addr:%lu size:%lu\n",
176		  pos, size);
177
178	/* We don't want the framebuffer to be mapped encrypted */
179	vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
180
181	while (size > 0) {
182		page = vmalloc_to_pfn((void *)pos);
183		if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
184			return -EAGAIN;
185
186		start += PAGE_SIZE;
187		pos += PAGE_SIZE;
188		if (size > PAGE_SIZE)
189			size -= PAGE_SIZE;
190		else
191			size = 0;
192	}
193
194	/* VM_IO | VM_DONTEXPAND | VM_DONTDUMP are set by remap_pfn_range() */
195	return 0;
196}
197
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198/*
199 * It's common for several clients to have framebuffer open simultaneously.
200 * e.g. both fbcon and X. Makes things interesting.
201 * Assumes caller is holding info->lock (for open and release at least)
202 */
203static int udl_fb_open(struct fb_info *info, int user)
204{
205	struct udl_fbdev *ufbdev = info->par;
206	struct drm_device *dev = ufbdev->ufb.base.dev;
207	struct udl_device *udl = dev->dev_private;
208
209	/* If the USB device is gone, we don't accept new opens */
210	if (drm_dev_is_unplugged(udl->ddev))
211		return -ENODEV;
212
213	ufbdev->fb_count++;
214
215#ifdef CONFIG_DRM_FBDEV_EMULATION
216	if (fb_defio && (info->fbdefio == NULL)) {
217		/* enable defio at last moment if not disabled by client */
218
219		struct fb_deferred_io *fbdefio;
220
221		fbdefio = kmalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
222
223		if (fbdefio) {
224			fbdefio->delay = DL_DEFIO_WRITE_DELAY;
225			fbdefio->deferred_io = drm_fb_helper_deferred_io;
226		}
227
228		info->fbdefio = fbdefio;
229		fb_deferred_io_init(info);
230	}
231#endif
232
233	pr_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
234		  info->node, user, info, ufbdev->fb_count);
235
236	return 0;
237}
238
239
240/*
241 * Assumes caller is holding info->lock mutex (for open and release at least)
242 */
243static int udl_fb_release(struct fb_info *info, int user)
244{
245	struct udl_fbdev *ufbdev = info->par;
246
247	ufbdev->fb_count--;
248
249#ifdef CONFIG_DRM_FBDEV_EMULATION
250	if ((ufbdev->fb_count == 0) && (info->fbdefio)) {
251		fb_deferred_io_cleanup(info);
252		kfree(info->fbdefio);
253		info->fbdefio = NULL;
254		info->fbops->fb_mmap = udl_fb_mmap;
255	}
256#endif
257
258	pr_warn("released /dev/fb%d user=%d count=%d\n",
259		info->node, user, ufbdev->fb_count);
260
261	return 0;
262}
263
264static struct fb_ops udlfb_ops = {
265	.owner = THIS_MODULE,
266	DRM_FB_HELPER_DEFAULT_OPS,
267	.fb_fillrect = drm_fb_helper_sys_fillrect,
268	.fb_copyarea = drm_fb_helper_sys_copyarea,
269	.fb_imageblit = drm_fb_helper_sys_imageblit,
 
 
 
 
 
 
270	.fb_mmap = udl_fb_mmap,
271	.fb_open = udl_fb_open,
272	.fb_release = udl_fb_release,
273};
274
 
 
 
 
 
 
 
 
 
 
 
 
 
275static int udl_user_framebuffer_dirty(struct drm_framebuffer *fb,
276				      struct drm_file *file,
277				      unsigned flags, unsigned color,
278				      struct drm_clip_rect *clips,
279				      unsigned num_clips)
280{
281	struct udl_framebuffer *ufb = to_udl_fb(fb);
282	int i;
283	int ret = 0;
284
285	drm_modeset_lock_all(fb->dev);
286
287	if (!ufb->active_16)
288		goto unlock;
289
290	if (ufb->obj->base.import_attach) {
291		ret = dma_buf_begin_cpu_access(ufb->obj->base.import_attach->dmabuf,
292					       DMA_FROM_DEVICE);
293		if (ret)
294			goto unlock;
295	}
296
297	for (i = 0; i < num_clips; i++) {
298		ret = udl_handle_damage(ufb, clips[i].x1, clips[i].y1,
299				  clips[i].x2 - clips[i].x1,
300				  clips[i].y2 - clips[i].y1);
301		if (ret)
302			break;
303	}
304
305	if (ufb->obj->base.import_attach) {
306		ret = dma_buf_end_cpu_access(ufb->obj->base.import_attach->dmabuf,
307					     DMA_FROM_DEVICE);
308	}
309
310 unlock:
311	drm_modeset_unlock_all(fb->dev);
312
313	return ret;
314}
315
316static void udl_user_framebuffer_destroy(struct drm_framebuffer *fb)
317{
318	struct udl_framebuffer *ufb = to_udl_fb(fb);
319
320	if (ufb->obj)
321		drm_gem_object_put_unlocked(&ufb->obj->base);
322
323	drm_framebuffer_cleanup(fb);
324	kfree(ufb);
325}
326
327static const struct drm_framebuffer_funcs udlfb_funcs = {
328	.destroy = udl_user_framebuffer_destroy,
329	.dirty = udl_user_framebuffer_dirty,
 
330};
331
332
333static int
334udl_framebuffer_init(struct drm_device *dev,
335		     struct udl_framebuffer *ufb,
336		     const struct drm_mode_fb_cmd2 *mode_cmd,
337		     struct udl_gem_object *obj)
338{
339	int ret;
340
341	ufb->obj = obj;
342	drm_helper_mode_fill_fb_struct(dev, &ufb->base, mode_cmd);
343	ret = drm_framebuffer_init(dev, &ufb->base, &udlfb_funcs);
 
344	return ret;
345}
346
347
348static int udlfb_create(struct drm_fb_helper *helper,
349			struct drm_fb_helper_surface_size *sizes)
350{
351	struct udl_fbdev *ufbdev =
352		container_of(helper, struct udl_fbdev, helper);
353	struct drm_device *dev = ufbdev->helper.dev;
354	struct fb_info *info;
 
355	struct drm_framebuffer *fb;
356	struct drm_mode_fb_cmd2 mode_cmd;
357	struct udl_gem_object *obj;
358	uint32_t size;
359	int ret = 0;
360
361	if (sizes->surface_bpp == 24)
362		sizes->surface_bpp = 32;
363
364	mode_cmd.width = sizes->surface_width;
365	mode_cmd.height = sizes->surface_height;
366	mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
367
368	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
369							  sizes->surface_depth);
370
371	size = mode_cmd.pitches[0] * mode_cmd.height;
372	size = ALIGN(size, PAGE_SIZE);
373
374	obj = udl_gem_alloc_object(dev, size);
375	if (!obj)
376		goto out;
377
378	ret = udl_gem_vmap(obj);
379	if (ret) {
380		DRM_ERROR("failed to vmap fb\n");
381		goto out_gfree;
382	}
383
384	info = drm_fb_helper_alloc_fbi(helper);
385	if (IS_ERR(info)) {
386		ret = PTR_ERR(info);
387		goto out_gfree;
388	}
389	info->par = ufbdev;
390
391	ret = udl_framebuffer_init(dev, &ufbdev->ufb, &mode_cmd, obj);
392	if (ret)
393		goto out_gfree;
394
395	fb = &ufbdev->ufb.base;
396
397	ufbdev->helper.fb = fb;
 
398
399	strcpy(info->fix.id, "udldrmfb");
400
401	info->screen_base = ufbdev->ufb.obj->vmapping;
402	info->fix.smem_len = size;
403	info->fix.smem_start = (unsigned long)ufbdev->ufb.obj->vmapping;
404
 
405	info->fbops = &udlfb_ops;
406	drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
407	drm_fb_helper_fill_var(info, &ufbdev->helper, sizes->fb_width, sizes->fb_height);
408
 
 
 
 
 
 
 
409	DRM_DEBUG_KMS("allocated %dx%d vmal %p\n",
410		      fb->width, fb->height,
411		      ufbdev->ufb.obj->vmapping);
412
413	return ret;
414out_gfree:
415	drm_gem_object_put_unlocked(&ufbdev->ufb.obj->base);
416out:
417	return ret;
418}
419
420static const struct drm_fb_helper_funcs udl_fb_helper_funcs = {
421	.fb_probe = udlfb_create,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
422};
423
424static void udl_fbdev_destroy(struct drm_device *dev,
425			      struct udl_fbdev *ufbdev)
426{
427	drm_fb_helper_unregister_fbi(&ufbdev->helper);
 
 
 
 
 
 
 
428	drm_fb_helper_fini(&ufbdev->helper);
429	drm_framebuffer_unregister_private(&ufbdev->ufb.base);
430	drm_framebuffer_cleanup(&ufbdev->ufb.base);
431	drm_gem_object_put_unlocked(&ufbdev->ufb.obj->base);
432}
433
434int udl_fbdev_init(struct drm_device *dev)
435{
436	struct udl_device *udl = dev->dev_private;
437	int bpp_sel = fb_bpp;
438	struct udl_fbdev *ufbdev;
439	int ret;
440
441	ufbdev = kzalloc(sizeof(struct udl_fbdev), GFP_KERNEL);
442	if (!ufbdev)
443		return -ENOMEM;
444
445	udl->fbdev = ufbdev;
 
446
447	drm_fb_helper_prepare(dev, &ufbdev->helper, &udl_fb_helper_funcs);
448
449	ret = drm_fb_helper_init(dev, &ufbdev->helper, 1);
450	if (ret)
451		goto free;
452
453	ret = drm_fb_helper_single_add_all_connectors(&ufbdev->helper);
454	if (ret)
455		goto fini;
456
457	/* disable all the possible outputs/crtcs before entering KMS mode */
458	drm_helper_disable_unused_functions(dev);
459
460	ret = drm_fb_helper_initial_config(&ufbdev->helper, bpp_sel);
461	if (ret)
462		goto fini;
463
 
 
464	return 0;
465
466fini:
467	drm_fb_helper_fini(&ufbdev->helper);
468free:
469	kfree(ufbdev);
470	return ret;
471}
472
473void udl_fbdev_cleanup(struct drm_device *dev)
474{
475	struct udl_device *udl = dev->dev_private;
476	if (!udl->fbdev)
477		return;
478
479	udl_fbdev_destroy(dev, udl->fbdev);
480	kfree(udl->fbdev);
481	udl->fbdev = NULL;
482}
483
484void udl_fbdev_unplug(struct drm_device *dev)
485{
486	struct udl_device *udl = dev->dev_private;
487	struct udl_fbdev *ufbdev;
488	if (!udl->fbdev)
489		return;
490
491	ufbdev = udl->fbdev;
492	drm_fb_helper_unlink_fbi(&ufbdev->helper);
 
 
 
 
493}
494
495struct drm_framebuffer *
496udl_fb_user_fb_create(struct drm_device *dev,
497		   struct drm_file *file,
498		   const struct drm_mode_fb_cmd2 *mode_cmd)
499{
500	struct drm_gem_object *obj;
501	struct udl_framebuffer *ufb;
502	int ret;
503	uint32_t size;
504
505	obj = drm_gem_object_lookup(file, mode_cmd->handles[0]);
506	if (obj == NULL)
507		return ERR_PTR(-ENOENT);
508
509	size = mode_cmd->pitches[0] * mode_cmd->height;
510	size = ALIGN(size, PAGE_SIZE);
511
512	if (size > obj->size) {
513		DRM_ERROR("object size not sufficient for fb %d %zu %d %d\n", size, obj->size, mode_cmd->pitches[0], mode_cmd->height);
514		return ERR_PTR(-ENOMEM);
515	}
516
517	ufb = kzalloc(sizeof(*ufb), GFP_KERNEL);
518	if (ufb == NULL)
519		return ERR_PTR(-ENOMEM);
520
521	ret = udl_framebuffer_init(dev, ufb, mode_cmd, to_udl_bo(obj));
522	if (ret) {
523		kfree(ufb);
524		return ERR_PTR(-EINVAL);
525	}
526	return &ufb->base;
527}
v3.5.6
  1/*
  2 * Copyright (C) 2012 Red Hat
  3 *
  4 * based in parts on udlfb.c:
  5 * Copyright (C) 2009 Roberto De Ioris <roberto@unbit.it>
  6 * Copyright (C) 2009 Jaya Kumar <jayakumar.lkml@gmail.com>
  7 * Copyright (C) 2009 Bernie Thompson <bernie@plugable.com>
  8 *
  9 * This file is subject to the terms and conditions of the GNU General Public
 10 * License v2. See the file COPYING in the main directory of this archive for
 11 * more details.
 12 */
 13#include <linux/module.h>
 14#include <linux/slab.h>
 15#include <linux/fb.h>
 
 
 16
 17#include "drmP.h"
 18#include "drm.h"
 19#include "drm_crtc.h"
 20#include "drm_crtc_helper.h"
 21#include "udl_drv.h"
 22
 23#include "drm_fb_helper.h"
 24
 25#define DL_DEFIO_WRITE_DELAY    5 /* fb_deferred_io.delay in jiffies */
 26
 27static int fb_defio = 1;  /* Optionally enable experimental fb_defio mmap support */
 28static int fb_bpp = 16;
 29
 30module_param(fb_bpp, int, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
 31module_param(fb_defio, int, S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP);
 32
 33struct udl_fbdev {
 34	struct drm_fb_helper helper;
 35	struct udl_framebuffer ufb;
 36	struct list_head fbdev_list;
 37	int fb_count;
 38};
 39
 40#define DL_ALIGN_UP(x, a) ALIGN(x, a)
 41#define DL_ALIGN_DOWN(x, a) ALIGN(x-(a-1), a)
 42
 43/** Read the red component (0..255) of a 32 bpp colour. */
 44#define DLO_RGB_GETRED(col) (uint8_t)((col) & 0xFF)
 45
 46/** Read the green component (0..255) of a 32 bpp colour. */
 47#define DLO_RGB_GETGRN(col) (uint8_t)(((col) >> 8) & 0xFF)
 48
 49/** Read the blue component (0..255) of a 32 bpp colour. */
 50#define DLO_RGB_GETBLU(col) (uint8_t)(((col) >> 16) & 0xFF)
 51
 52/** Return red/green component of a 16 bpp colour number. */
 53#define DLO_RG16(red, grn) (uint8_t)((((red) & 0xF8) | ((grn) >> 5)) & 0xFF)
 54
 55/** Return green/blue component of a 16 bpp colour number. */
 56#define DLO_GB16(grn, blu) (uint8_t)(((((grn) & 0x1C) << 3) | ((blu) >> 3)) & 0xFF)
 57
 58/** Return 8 bpp colour number from red, green and blue components. */
 59#define DLO_RGB8(red, grn, blu) ((((red) << 5) | (((grn) & 3) << 3) | ((blu) & 7)) & 0xFF)
 60
 61#if 0
 62static uint8_t rgb8(uint32_t col)
 63{
 64	uint8_t red = DLO_RGB_GETRED(col);
 65	uint8_t grn = DLO_RGB_GETGRN(col);
 66	uint8_t blu = DLO_RGB_GETBLU(col);
 67
 68	return DLO_RGB8(red, grn, blu);
 69}
 70
 71static uint16_t rgb16(uint32_t col)
 72{
 73	uint8_t red = DLO_RGB_GETRED(col);
 74	uint8_t grn = DLO_RGB_GETGRN(col);
 75	uint8_t blu = DLO_RGB_GETBLU(col);
 76
 77	return (DLO_RG16(red, grn) << 8) + DLO_GB16(grn, blu);
 78}
 79#endif
 80
 81/*
 82 * NOTE: fb_defio.c is holding info->fbdefio.mutex
 83 *   Touching ANY framebuffer memory that triggers a page fault
 84 *   in fb_defio will cause a deadlock, when it also tries to
 85 *   grab the same mutex.
 86 */
 87static void udlfb_dpy_deferred_io(struct fb_info *info,
 88				  struct list_head *pagelist)
 89{
 90	struct page *cur;
 91	struct fb_deferred_io *fbdefio = info->fbdefio;
 92	struct udl_fbdev *ufbdev = info->par;
 93	struct drm_device *dev = ufbdev->ufb.base.dev;
 94	struct udl_device *udl = dev->dev_private;
 95	struct urb *urb;
 96	char *cmd;
 97	cycles_t start_cycles, end_cycles;
 98	int bytes_sent = 0;
 99	int bytes_identical = 0;
100	int bytes_rendered = 0;
101
102	if (!fb_defio)
103		return;
104
105	start_cycles = get_cycles();
106
107	urb = udl_get_urb(dev);
108	if (!urb)
109		return;
110
111	cmd = urb->transfer_buffer;
112
113	/* walk the written page list and render each to device */
114	list_for_each_entry(cur, &fbdefio->pagelist, lru) {
115
116		if (udl_render_hline(dev, (ufbdev->ufb.base.bits_per_pixel / 8),
117				  &urb, (char *) info->fix.smem_start,
118				  &cmd, cur->index << PAGE_SHIFT,
119				  PAGE_SIZE, &bytes_identical, &bytes_sent))
120			goto error;
121		bytes_rendered += PAGE_SIZE;
122	}
123
124	if (cmd > (char *) urb->transfer_buffer) {
125		/* Send partial buffer remaining before exiting */
126		int len = cmd - (char *) urb->transfer_buffer;
127		udl_submit_urb(dev, urb, len);
128		bytes_sent += len;
129	} else
130		udl_urb_completion(urb);
131
132error:
133	atomic_add(bytes_sent, &udl->bytes_sent);
134	atomic_add(bytes_identical, &udl->bytes_identical);
135	atomic_add(bytes_rendered, &udl->bytes_rendered);
136	end_cycles = get_cycles();
137	atomic_add(((unsigned int) ((end_cycles - start_cycles)
138		    >> 10)), /* Kcycles */
139		   &udl->cpu_kcycles_used);
140}
141
142int udl_handle_damage(struct udl_framebuffer *fb, int x, int y,
143		      int width, int height)
144{
145	struct drm_device *dev = fb->base.dev;
146	struct udl_device *udl = dev->dev_private;
147	int i, ret;
148	char *cmd;
149	cycles_t start_cycles, end_cycles;
150	int bytes_sent = 0;
151	int bytes_identical = 0;
152	struct urb *urb;
153	int aligned_x;
154	int bpp = (fb->base.bits_per_pixel / 8);
155
156	if (!fb->active_16)
157		return 0;
158
159	if (!fb->obj->vmapping) {
160		ret = udl_gem_vmap(fb->obj);
161		if (ret == -ENOMEM) {
162			DRM_ERROR("failed to vmap fb\n");
163			return 0;
164		}
165		if (!fb->obj->vmapping) {
166			DRM_ERROR("failed to vmapping\n");
167			return 0;
168		}
169	}
170
171	start_cycles = get_cycles();
172
173	aligned_x = DL_ALIGN_DOWN(x, sizeof(unsigned long));
174	width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
175	x = aligned_x;
176
177	if ((width <= 0) ||
178	    (x + width > fb->base.width) ||
179	    (y + height > fb->base.height))
180		return -EINVAL;
181
 
 
182	urb = udl_get_urb(dev);
183	if (!urb)
184		return 0;
185	cmd = urb->transfer_buffer;
186
187	for (i = y; i < y + height ; i++) {
188		const int line_offset = fb->base.pitches[0] * i;
189		const int byte_offset = line_offset + (x * bpp);
190
191		if (udl_render_hline(dev, bpp, &urb,
192				     (char *) fb->obj->vmapping,
193				     &cmd, byte_offset, width * bpp,
 
194				     &bytes_identical, &bytes_sent))
195			goto error;
196	}
197
198	if (cmd > (char *) urb->transfer_buffer) {
199		/* Send partial buffer remaining before exiting */
200		int len = cmd - (char *) urb->transfer_buffer;
201		ret = udl_submit_urb(dev, urb, len);
202		bytes_sent += len;
203	} else
204		udl_urb_completion(urb);
205
206error:
207	atomic_add(bytes_sent, &udl->bytes_sent);
208	atomic_add(bytes_identical, &udl->bytes_identical);
209	atomic_add(width*height*bpp, &udl->bytes_rendered);
210	end_cycles = get_cycles();
211	atomic_add(((unsigned int) ((end_cycles - start_cycles)
212		    >> 10)), /* Kcycles */
213		   &udl->cpu_kcycles_used);
214
215	return 0;
216}
217
218static int udl_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
219{
220	unsigned long start = vma->vm_start;
221	unsigned long size = vma->vm_end - vma->vm_start;
222	unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
223	unsigned long page, pos;
224
225	if (offset + size > info->fix.smem_len)
 
 
 
 
 
226		return -EINVAL;
227
228	pos = (unsigned long)info->fix.smem_start + offset;
229
230	pr_notice("mmap() framebuffer addr:%lu size:%lu\n",
231		  pos, size);
232
 
 
 
233	while (size > 0) {
234		page = vmalloc_to_pfn((void *)pos);
235		if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
236			return -EAGAIN;
237
238		start += PAGE_SIZE;
239		pos += PAGE_SIZE;
240		if (size > PAGE_SIZE)
241			size -= PAGE_SIZE;
242		else
243			size = 0;
244	}
245
246	vma->vm_flags |= VM_RESERVED;	/* avoid to swap out this VMA */
247	return 0;
248}
249
250static void udl_fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
251{
252	struct udl_fbdev *ufbdev = info->par;
253
254	sys_fillrect(info, rect);
255
256	udl_handle_damage(&ufbdev->ufb, rect->dx, rect->dy, rect->width,
257			  rect->height);
258}
259
260static void udl_fb_copyarea(struct fb_info *info, const struct fb_copyarea *region)
261{
262	struct udl_fbdev *ufbdev = info->par;
263
264	sys_copyarea(info, region);
265
266	udl_handle_damage(&ufbdev->ufb, region->dx, region->dy, region->width,
267			  region->height);
268}
269
270static void udl_fb_imageblit(struct fb_info *info, const struct fb_image *image)
271{
272	struct udl_fbdev *ufbdev = info->par;
273
274	sys_imageblit(info, image);
275
276	udl_handle_damage(&ufbdev->ufb, image->dx, image->dy, image->width,
277			  image->height);
278}
279
280/*
281 * It's common for several clients to have framebuffer open simultaneously.
282 * e.g. both fbcon and X. Makes things interesting.
283 * Assumes caller is holding info->lock (for open and release at least)
284 */
285static int udl_fb_open(struct fb_info *info, int user)
286{
287	struct udl_fbdev *ufbdev = info->par;
288	struct drm_device *dev = ufbdev->ufb.base.dev;
289	struct udl_device *udl = dev->dev_private;
290
291	/* If the USB device is gone, we don't accept new opens */
292	if (drm_device_is_unplugged(udl->ddev))
293		return -ENODEV;
294
295	ufbdev->fb_count++;
296
 
297	if (fb_defio && (info->fbdefio == NULL)) {
298		/* enable defio at last moment if not disabled by client */
299
300		struct fb_deferred_io *fbdefio;
301
302		fbdefio = kmalloc(sizeof(struct fb_deferred_io), GFP_KERNEL);
303
304		if (fbdefio) {
305			fbdefio->delay = DL_DEFIO_WRITE_DELAY;
306			fbdefio->deferred_io = udlfb_dpy_deferred_io;
307		}
308
309		info->fbdefio = fbdefio;
310		fb_deferred_io_init(info);
311	}
 
312
313	pr_notice("open /dev/fb%d user=%d fb_info=%p count=%d\n",
314		  info->node, user, info, ufbdev->fb_count);
315
316	return 0;
317}
318
319
320/*
321 * Assumes caller is holding info->lock mutex (for open and release at least)
322 */
323static int udl_fb_release(struct fb_info *info, int user)
324{
325	struct udl_fbdev *ufbdev = info->par;
326
327	ufbdev->fb_count--;
328
 
329	if ((ufbdev->fb_count == 0) && (info->fbdefio)) {
330		fb_deferred_io_cleanup(info);
331		kfree(info->fbdefio);
332		info->fbdefio = NULL;
333		info->fbops->fb_mmap = udl_fb_mmap;
334	}
 
335
336	pr_warn("released /dev/fb%d user=%d count=%d\n",
337		info->node, user, ufbdev->fb_count);
338
339	return 0;
340}
341
342static struct fb_ops udlfb_ops = {
343	.owner = THIS_MODULE,
344	.fb_check_var = drm_fb_helper_check_var,
345	.fb_set_par = drm_fb_helper_set_par,
346	.fb_fillrect = udl_fb_fillrect,
347	.fb_copyarea = udl_fb_copyarea,
348	.fb_imageblit = udl_fb_imageblit,
349	.fb_pan_display = drm_fb_helper_pan_display,
350	.fb_blank = drm_fb_helper_blank,
351	.fb_setcmap = drm_fb_helper_setcmap,
352	.fb_debug_enter = drm_fb_helper_debug_enter,
353	.fb_debug_leave = drm_fb_helper_debug_leave,
354	.fb_mmap = udl_fb_mmap,
355	.fb_open = udl_fb_open,
356	.fb_release = udl_fb_release,
357};
358
359void udl_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
360			   u16 blue, int regno)
361{
362}
363
364void udl_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
365			     u16 *blue, int regno)
366{
367	*red = 0;
368	*green = 0;
369	*blue = 0;
370}
371
372static int udl_user_framebuffer_dirty(struct drm_framebuffer *fb,
373				      struct drm_file *file,
374				      unsigned flags, unsigned color,
375				      struct drm_clip_rect *clips,
376				      unsigned num_clips)
377{
378	struct udl_framebuffer *ufb = to_udl_fb(fb);
379	int i;
 
 
 
380
381	if (!ufb->active_16)
382		return 0;
 
 
 
 
 
 
 
383
384	for (i = 0; i < num_clips; i++) {
385		udl_handle_damage(ufb, clips[i].x1, clips[i].y1,
386				  clips[i].x2 - clips[i].x1,
387				  clips[i].y2 - clips[i].y1);
 
 
388	}
389	return 0;
 
 
 
 
 
 
 
 
 
390}
391
392static void udl_user_framebuffer_destroy(struct drm_framebuffer *fb)
393{
394	struct udl_framebuffer *ufb = to_udl_fb(fb);
395
396	if (ufb->obj)
397		drm_gem_object_unreference_unlocked(&ufb->obj->base);
398
399	drm_framebuffer_cleanup(fb);
400	kfree(ufb);
401}
402
403static const struct drm_framebuffer_funcs udlfb_funcs = {
404	.destroy = udl_user_framebuffer_destroy,
405	.dirty = udl_user_framebuffer_dirty,
406	.create_handle = NULL,
407};
408
409
410static int
411udl_framebuffer_init(struct drm_device *dev,
412		     struct udl_framebuffer *ufb,
413		     struct drm_mode_fb_cmd2 *mode_cmd,
414		     struct udl_gem_object *obj)
415{
416	int ret;
417
418	ufb->obj = obj;
 
419	ret = drm_framebuffer_init(dev, &ufb->base, &udlfb_funcs);
420	drm_helper_mode_fill_fb_struct(&ufb->base, mode_cmd);
421	return ret;
422}
423
424
425static int udlfb_create(struct udl_fbdev *ufbdev,
426			struct drm_fb_helper_surface_size *sizes)
427{
 
 
428	struct drm_device *dev = ufbdev->helper.dev;
429	struct fb_info *info;
430	struct device *device = &dev->usbdev->dev;
431	struct drm_framebuffer *fb;
432	struct drm_mode_fb_cmd2 mode_cmd;
433	struct udl_gem_object *obj;
434	uint32_t size;
435	int ret = 0;
436
437	if (sizes->surface_bpp == 24)
438		sizes->surface_bpp = 32;
439
440	mode_cmd.width = sizes->surface_width;
441	mode_cmd.height = sizes->surface_height;
442	mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
443
444	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
445							  sizes->surface_depth);
446
447	size = mode_cmd.pitches[0] * mode_cmd.height;
448	size = ALIGN(size, PAGE_SIZE);
449
450	obj = udl_gem_alloc_object(dev, size);
451	if (!obj)
452		goto out;
453
454	ret = udl_gem_vmap(obj);
455	if (ret) {
456		DRM_ERROR("failed to vmap fb\n");
457		goto out_gfree;
458	}
459
460	info = framebuffer_alloc(0, device);
461	if (!info) {
462		ret = -ENOMEM;
463		goto out_gfree;
464	}
465	info->par = ufbdev;
466
467	ret = udl_framebuffer_init(dev, &ufbdev->ufb, &mode_cmd, obj);
468	if (ret)
469		goto out_gfree;
470
471	fb = &ufbdev->ufb.base;
472
473	ufbdev->helper.fb = fb;
474	ufbdev->helper.fbdev = info;
475
476	strcpy(info->fix.id, "udldrmfb");
477
478	info->screen_base = ufbdev->ufb.obj->vmapping;
479	info->fix.smem_len = size;
480	info->fix.smem_start = (unsigned long)ufbdev->ufb.obj->vmapping;
481
482	info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
483	info->fbops = &udlfb_ops;
484	drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
485	drm_fb_helper_fill_var(info, &ufbdev->helper, sizes->fb_width, sizes->fb_height);
486
487	ret = fb_alloc_cmap(&info->cmap, 256, 0);
488	if (ret) {
489		ret = -ENOMEM;
490		goto out_gfree;
491	}
492
493
494	DRM_DEBUG_KMS("allocated %dx%d vmal %p\n",
495		      fb->width, fb->height,
496		      ufbdev->ufb.obj->vmapping);
497
498	return ret;
499out_gfree:
500	drm_gem_object_unreference(&ufbdev->ufb.obj->base);
501out:
502	return ret;
503}
504
505static int udl_fb_find_or_create_single(struct drm_fb_helper *helper,
506					struct drm_fb_helper_surface_size *sizes)
507{
508	struct udl_fbdev *ufbdev = (struct udl_fbdev *)helper;
509	int new_fb = 0;
510	int ret;
511
512	if (!helper->fb) {
513		ret = udlfb_create(ufbdev, sizes);
514		if (ret)
515			return ret;
516
517		new_fb = 1;
518	}
519	return new_fb;
520}
521
522static struct drm_fb_helper_funcs udl_fb_helper_funcs = {
523	.gamma_set = udl_crtc_fb_gamma_set,
524	.gamma_get = udl_crtc_fb_gamma_get,
525	.fb_probe = udl_fb_find_or_create_single,
526};
527
528static void udl_fbdev_destroy(struct drm_device *dev,
529			      struct udl_fbdev *ufbdev)
530{
531	struct fb_info *info;
532	if (ufbdev->helper.fbdev) {
533		info = ufbdev->helper.fbdev;
534		unregister_framebuffer(info);
535		if (info->cmap.len)
536			fb_dealloc_cmap(&info->cmap);
537		framebuffer_release(info);
538	}
539	drm_fb_helper_fini(&ufbdev->helper);
 
540	drm_framebuffer_cleanup(&ufbdev->ufb.base);
541	drm_gem_object_unreference_unlocked(&ufbdev->ufb.obj->base);
542}
543
544int udl_fbdev_init(struct drm_device *dev)
545{
546	struct udl_device *udl = dev->dev_private;
547	int bpp_sel = fb_bpp;
548	struct udl_fbdev *ufbdev;
549	int ret;
550
551	ufbdev = kzalloc(sizeof(struct udl_fbdev), GFP_KERNEL);
552	if (!ufbdev)
553		return -ENOMEM;
554
555	udl->fbdev = ufbdev;
556	ufbdev->helper.funcs = &udl_fb_helper_funcs;
557
558	ret = drm_fb_helper_init(dev, &ufbdev->helper,
559				 1, 1);
560	if (ret) {
561		kfree(ufbdev);
562		return ret;
 
 
 
 
 
 
 
563
564	}
 
 
565
566	drm_fb_helper_single_add_all_connectors(&ufbdev->helper);
567	drm_fb_helper_initial_config(&ufbdev->helper, bpp_sel);
568	return 0;
 
 
 
 
 
 
569}
570
571void udl_fbdev_cleanup(struct drm_device *dev)
572{
573	struct udl_device *udl = dev->dev_private;
574	if (!udl->fbdev)
575		return;
576
577	udl_fbdev_destroy(dev, udl->fbdev);
578	kfree(udl->fbdev);
579	udl->fbdev = NULL;
580}
581
582void udl_fbdev_unplug(struct drm_device *dev)
583{
584	struct udl_device *udl = dev->dev_private;
585	struct udl_fbdev *ufbdev;
586	if (!udl->fbdev)
587		return;
588
589	ufbdev = udl->fbdev;
590	if (ufbdev->helper.fbdev) {
591		struct fb_info *info;
592		info = ufbdev->helper.fbdev;
593		unlink_framebuffer(info);
594	}
595}
596
597struct drm_framebuffer *
598udl_fb_user_fb_create(struct drm_device *dev,
599		   struct drm_file *file,
600		   struct drm_mode_fb_cmd2 *mode_cmd)
601{
602	struct drm_gem_object *obj;
603	struct udl_framebuffer *ufb;
604	int ret;
605	uint32_t size;
606
607	obj = drm_gem_object_lookup(dev, file, mode_cmd->handles[0]);
608	if (obj == NULL)
609		return ERR_PTR(-ENOENT);
610
611	size = mode_cmd->pitches[0] * mode_cmd->height;
612	size = ALIGN(size, PAGE_SIZE);
613
614	if (size > obj->size) {
615		DRM_ERROR("object size not sufficient for fb %d %zu %d %d\n", size, obj->size, mode_cmd->pitches[0], mode_cmd->height);
616		return ERR_PTR(-ENOMEM);
617	}
618
619	ufb = kzalloc(sizeof(*ufb), GFP_KERNEL);
620	if (ufb == NULL)
621		return ERR_PTR(-ENOMEM);
622
623	ret = udl_framebuffer_init(dev, ufb, mode_cmd, to_udl_bo(obj));
624	if (ret) {
625		kfree(ufb);
626		return ERR_PTR(-EINVAL);
627	}
628	return &ufb->base;
629}