2 * Copyright (C) 2013 Google, Inc.
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
15 #include <linux/kthread.h>
16 #include <linux/mutex.h>
17 #include <linux/slab.h>
21 #include <video/adf.h>
22 #include <video/adf_client.h>
23 #include <video/adf_format.h>
27 static inline bool vsync_active(u8 state)
29 return state == DRM_MODE_DPMS_ON || state == DRM_MODE_DPMS_STANDBY;
33 * adf_interface_blank - set interface's DPMS state
35 * @intf: the interface
36 * @state: one of %DRM_MODE_DPMS_*
38 * Returns 0 on success or -errno on failure.
40 int adf_interface_blank(struct adf_interface *intf, u8 state)
42 struct adf_device *dev = adf_interface_parent(intf);
47 struct adf_event_refcount *vsync_refcount;
49 if (!intf->ops || !intf->ops->blank)
52 mutex_lock(&dev->client_lock);
53 if (state != DRM_MODE_DPMS_ON)
54 flush_kthread_worker(&dev->post_worker);
55 mutex_lock(&intf->base.event_lock);
57 vsync_refcount = adf_obj_find_event_refcount(&intf->base,
59 if (!vsync_refcount) {
64 prev_state = intf->dpms_state;
65 if (prev_state == state) {
70 disable_vsync = vsync_active(prev_state) &&
71 !vsync_active(state) &&
72 vsync_refcount->refcount;
73 enable_vsync = !vsync_active(prev_state) &&
74 vsync_active(state) &&
75 vsync_refcount->refcount;
78 intf->base.ops->set_event(&intf->base, ADF_EVENT_VSYNC,
81 ret = intf->ops->blank(intf, state);
84 intf->base.ops->set_event(&intf->base, ADF_EVENT_VSYNC,
90 intf->base.ops->set_event(&intf->base, ADF_EVENT_VSYNC,
93 intf->dpms_state = state;
95 mutex_unlock(&intf->base.event_lock);
96 mutex_unlock(&dev->client_lock);
99 EXPORT_SYMBOL(adf_interface_blank);
102 * adf_interface_blank - get interface's current DPMS state
104 * @intf: the interface
106 * Returns one of %DRM_MODE_DPMS_*.
108 u8 adf_interface_dpms_state(struct adf_interface *intf)
110 struct adf_device *dev = adf_interface_parent(intf);
113 mutex_lock(&dev->client_lock);
114 dpms_state = intf->dpms_state;
115 mutex_unlock(&dev->client_lock);
119 EXPORT_SYMBOL(adf_interface_dpms_state);
122 * adf_interface_current_mode - get interface's current display mode
124 * @intf: the interface
125 * @mode: returns the current mode
127 void adf_interface_current_mode(struct adf_interface *intf,
128 struct drm_mode_modeinfo *mode)
130 struct adf_device *dev = adf_interface_parent(intf);
132 mutex_lock(&dev->client_lock);
133 memcpy(mode, &intf->current_mode, sizeof(*mode));
134 mutex_unlock(&dev->client_lock);
136 EXPORT_SYMBOL(adf_interface_current_mode);
139 * adf_interface_modelist - get interface's modelist
141 * @intf: the interface
142 * @modelist: storage for the modelist (optional)
143 * @n_modes: length of @modelist
145 * If @modelist is not NULL, adf_interface_modelist() will copy up to @n_modes
146 * modelist entries into @modelist.
148 * Returns the length of the modelist.
150 size_t adf_interface_modelist(struct adf_interface *intf,
151 struct drm_mode_modeinfo *modelist, size_t n_modes)
156 read_lock_irqsave(&intf->hotplug_modelist_lock, flags);
158 memcpy(modelist, intf->modelist, sizeof(modelist[0]) *
159 min(n_modes, intf->n_modes));
160 retval = intf->n_modes;
161 read_unlock_irqrestore(&intf->hotplug_modelist_lock, flags);
165 EXPORT_SYMBOL(adf_interface_modelist);
168 * adf_interface_set_mode - set interface's display mode
170 * @intf: the interface
171 * @mode: the new mode
173 * Returns 0 on success or -errno on failure.
175 int adf_interface_set_mode(struct adf_interface *intf,
176 struct drm_mode_modeinfo *mode)
178 struct adf_device *dev = adf_interface_parent(intf);
181 if (!intf->ops || !intf->ops->modeset)
184 mutex_lock(&dev->client_lock);
185 flush_kthread_worker(&dev->post_worker);
187 ret = intf->ops->modeset(intf, mode);
191 memcpy(&intf->current_mode, mode, sizeof(*mode));
193 mutex_unlock(&dev->client_lock);
196 EXPORT_SYMBOL(adf_interface_set_mode);
199 * adf_interface_screen_size - get size of screen connected to interface
201 * @intf: the interface
202 * @width_mm: returns the screen width in mm
203 * @height_mm: returns the screen width in mm
205 * Returns 0 on success or -errno on failure.
207 int adf_interface_get_screen_size(struct adf_interface *intf, u16 *width_mm,
210 struct adf_device *dev = adf_interface_parent(intf);
213 if (!intf->ops || !intf->ops->screen_size)
216 mutex_lock(&dev->client_lock);
217 ret = intf->ops->screen_size(intf, width_mm, height_mm);
218 mutex_unlock(&dev->client_lock);
222 EXPORT_SYMBOL(adf_interface_get_screen_size);
225 * adf_overlay_engine_supports_format - returns whether a format is in an
226 * overlay engine's supported list
228 * @eng: the overlay engine
229 * @format: format fourcc
231 bool adf_overlay_engine_supports_format(struct adf_overlay_engine *eng,
235 for (i = 0; i < eng->ops->n_supported_formats; i++)
236 if (format == eng->ops->supported_formats[i])
241 EXPORT_SYMBOL(adf_overlay_engine_supports_format);
243 static int adf_buffer_validate(struct adf_buffer *buf)
245 struct adf_overlay_engine *eng = buf->overlay_engine;
246 struct device *dev = &eng->base.dev;
247 struct adf_device *parent = adf_overlay_engine_parent(eng);
248 u8 hsub, vsub, num_planes, cpp[ADF_MAX_PLANES], i;
250 if (!adf_overlay_engine_supports_format(eng, buf->format)) {
251 char format_str[ADF_FORMAT_STR_SIZE];
252 adf_format_str(buf->format, format_str);
253 dev_err(dev, "unsupported format %s\n", format_str);
257 if (!adf_format_is_standard(buf->format))
258 return parent->ops->validate_custom_format(parent, buf);
260 hsub = adf_format_horz_chroma_subsampling(buf->format);
261 vsub = adf_format_vert_chroma_subsampling(buf->format);
262 num_planes = adf_format_num_planes(buf->format);
263 for (i = 0; i < num_planes; i++)
264 cpp[i] = adf_format_plane_cpp(buf->format, i);
266 return adf_format_validate_yuv(parent, buf, num_planes, hsub, vsub,
270 static int adf_buffer_map(struct adf_device *dev, struct adf_buffer *buf,
271 struct adf_buffer_mapping *mapping)
276 for (i = 0; i < buf->n_planes; i++) {
277 struct dma_buf_attachment *attachment;
278 struct sg_table *sg_table;
280 attachment = dma_buf_attach(buf->dma_bufs[i], dev->dev);
281 if (IS_ERR(attachment)) {
282 ret = PTR_ERR(attachment);
283 dev_err(&dev->base.dev, "attaching plane %u failed: %d\n",
287 mapping->attachments[i] = attachment;
289 sg_table = dma_buf_map_attachment(attachment, DMA_TO_DEVICE);
290 if (IS_ERR(sg_table)) {
291 ret = PTR_ERR(sg_table);
292 dev_err(&dev->base.dev, "mapping plane %u failed: %d",
295 } else if (!sg_table) {
297 dev_err(&dev->base.dev, "mapping plane %u failed\n", i);
300 mapping->sg_tables[i] = sg_table;
305 adf_buffer_mapping_cleanup(mapping, buf);
310 static struct sync_fence *adf_sw_complete_fence(struct adf_device *dev)
313 struct sync_fence *complete_fence;
315 if (!dev->timeline) {
316 dev->timeline = sw_sync_timeline_create(dev->base.name);
318 return ERR_PTR(-ENOMEM);
319 dev->timeline_max = 1;
323 pt = sw_sync_pt_create(dev->timeline, dev->timeline_max);
326 complete_fence = sync_fence_create(dev->base.name, pt);
328 goto err_fence_create;
330 return complete_fence;
336 return ERR_PTR(-ENOSYS);
340 * adf_device_post - flip to a new set of buffers
342 * @dev: device targeted by the flip
343 * @intfs: interfaces targeted by the flip
344 * @n_intfs: number of targeted interfaces
345 * @bufs: description of buffers displayed
346 * @n_bufs: number of buffers displayed
347 * @custom_data: driver-private data
348 * @custom_data_size: size of driver-private data
350 * adf_device_post() will copy @intfs, @bufs, and @custom_data, so they may
351 * point to variables on the stack. adf_device_post() also takes its own
352 * reference on each of the dma-bufs in @bufs. The adf_device_post_nocopy()
353 * variant transfers ownership of these resources to ADF instead.
355 * On success, returns a sync fence which signals when the buffers are removed
356 * from the screen. On failure, returns ERR_PTR(-errno).
358 struct sync_fence *adf_device_post(struct adf_device *dev,
359 struct adf_interface **intfs, size_t n_intfs,
360 struct adf_buffer *bufs, size_t n_bufs, void *custom_data,
361 size_t custom_data_size)
363 struct adf_interface **intfs_copy = NULL;
364 struct adf_buffer *bufs_copy = NULL;
365 void *custom_data_copy = NULL;
366 struct sync_fence *ret;
369 intfs_copy = kzalloc(sizeof(intfs_copy[0]) * n_intfs, GFP_KERNEL);
371 return ERR_PTR(-ENOMEM);
373 bufs_copy = kzalloc(sizeof(bufs_copy[0]) * n_bufs, GFP_KERNEL);
375 ret = ERR_PTR(-ENOMEM);
379 custom_data_copy = kzalloc(custom_data_size, GFP_KERNEL);
380 if (!custom_data_copy) {
381 ret = ERR_PTR(-ENOMEM);
385 for (i = 0; i < n_bufs; i++) {
387 for (j = 0; j < bufs[i].n_planes; j++)
388 get_dma_buf(bufs[i].dma_bufs[j]);
391 memcpy(intfs_copy, intfs, sizeof(intfs_copy[0]) * n_intfs);
392 memcpy(bufs_copy, bufs, sizeof(bufs_copy[0]) * n_bufs);
393 memcpy(custom_data_copy, custom_data, custom_data_size);
395 ret = adf_device_post_nocopy(dev, intfs_copy, n_intfs, bufs_copy,
396 n_bufs, custom_data_copy, custom_data_size);
403 for (i = 0; i < n_bufs; i++) {
405 for (j = 0; j < bufs[i].n_planes; j++)
406 dma_buf_put(bufs[i].dma_bufs[j]);
409 kfree(custom_data_copy);
414 EXPORT_SYMBOL(adf_device_post);
417 * adf_device_post_nocopy - flip to a new set of buffers
419 * adf_device_post_nocopy() has the same behavior as adf_device_post(),
420 * except ADF does not copy @intfs, @bufs, or @custom_data, and it does
421 * not take an extra reference on the dma-bufs in @bufs.
423 * @intfs, @bufs, and @custom_data must point to buffers allocated by
424 * kmalloc(). On success, ADF takes ownership of these buffers and the dma-bufs
425 * in @bufs, and will kfree()/dma_buf_put() them when they are no longer needed.
426 * On failure, adf_device_post_nocopy() does NOT take ownership of these
427 * buffers or the dma-bufs, and the caller must clean them up.
429 * adf_device_post_nocopy() is mainly intended for implementing ADF's ioctls.
430 * Clients may find the nocopy variant useful in limited cases, but most should
431 * call adf_device_post() instead.
433 struct sync_fence *adf_device_post_nocopy(struct adf_device *dev,
434 struct adf_interface **intfs, size_t n_intfs,
435 struct adf_buffer *bufs, size_t n_bufs,
436 void *custom_data, size_t custom_data_size)
438 struct adf_pending_post *cfg;
439 struct adf_buffer_mapping *mappings;
440 struct sync_fence *ret;
444 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
446 return ERR_PTR(-ENOMEM);
448 mappings = kzalloc(sizeof(mappings[0]) * n_bufs, GFP_KERNEL);
450 ret = ERR_PTR(-ENOMEM);
454 mutex_lock(&dev->client_lock);
456 for (i = 0; i < n_bufs; i++) {
457 err = adf_buffer_validate(&bufs[i]);
463 err = adf_buffer_map(dev, &bufs[i], &mappings[i]);
470 INIT_LIST_HEAD(&cfg->head);
471 cfg->config.n_bufs = n_bufs;
472 cfg->config.bufs = bufs;
473 cfg->config.mappings = mappings;
474 cfg->config.custom_data = custom_data;
475 cfg->config.custom_data_size = custom_data_size;
477 err = dev->ops->validate(dev, &cfg->config, &cfg->state);
483 mutex_lock(&dev->post_lock);
485 if (dev->ops->complete_fence)
486 ret = dev->ops->complete_fence(dev, &cfg->config,
489 ret = adf_sw_complete_fence(dev);
494 list_add_tail(&cfg->head, &dev->post_list);
495 queue_kthread_work(&dev->post_worker, &dev->post_work);
496 mutex_unlock(&dev->post_lock);
497 mutex_unlock(&dev->client_lock);
502 mutex_unlock(&dev->post_lock);
505 for (i = 0; i < n_bufs; i++)
506 adf_buffer_mapping_cleanup(&mappings[i], &bufs[i]);
508 mutex_unlock(&dev->client_lock);
515 EXPORT_SYMBOL(adf_device_post_nocopy);
517 static void adf_attachment_list_to_array(struct adf_device *dev,
518 struct list_head *src, struct adf_attachment *dst, size_t size)
520 struct adf_attachment_list *entry;
526 list_for_each_entry(entry, src, head) {
529 dst[i] = entry->attachment;
535 * adf_device_attachments - get device's list of active attachments
538 * @attachments: storage for the attachment list (optional)
539 * @n_attachments: length of @attachments
541 * If @attachments is not NULL, adf_device_attachments() will copy up to
542 * @n_attachments entries into @attachments.
544 * Returns the length of the active attachment list.
546 size_t adf_device_attachments(struct adf_device *dev,
547 struct adf_attachment *attachments, size_t n_attachments)
551 mutex_lock(&dev->client_lock);
552 adf_attachment_list_to_array(dev, &dev->attached, attachments,
554 retval = dev->n_attached;
555 mutex_unlock(&dev->client_lock);
559 EXPORT_SYMBOL(adf_device_attachments);
562 * adf_device_attachments_allowed - get device's list of allowed attachments
565 * @attachments: storage for the attachment list (optional)
566 * @n_attachments: length of @attachments
568 * If @attachments is not NULL, adf_device_attachments_allowed() will copy up to
569 * @n_attachments entries into @attachments.
571 * Returns the length of the allowed attachment list.
573 size_t adf_device_attachments_allowed(struct adf_device *dev,
574 struct adf_attachment *attachments, size_t n_attachments)
578 mutex_lock(&dev->client_lock);
579 adf_attachment_list_to_array(dev, &dev->attach_allowed, attachments,
581 retval = dev->n_attach_allowed;
582 mutex_unlock(&dev->client_lock);
586 EXPORT_SYMBOL(adf_device_attachments_allowed);
589 * adf_device_attached - return whether an overlay engine and interface are
592 * @dev: the parent device
593 * @eng: the overlay engine
594 * @intf: the interface
596 bool adf_device_attached(struct adf_device *dev, struct adf_overlay_engine *eng,
597 struct adf_interface *intf)
599 struct adf_attachment_list *attachment;
601 mutex_lock(&dev->client_lock);
602 attachment = adf_attachment_find(&dev->attached, eng, intf);
603 mutex_unlock(&dev->client_lock);
605 return attachment != NULL;
607 EXPORT_SYMBOL(adf_device_attached);
610 * adf_device_attach_allowed - return whether the ADF device supports attaching
611 * an overlay engine and interface
613 * @dev: the parent device
614 * @eng: the overlay engine
615 * @intf: the interface
617 bool adf_device_attach_allowed(struct adf_device *dev,
618 struct adf_overlay_engine *eng, struct adf_interface *intf)
620 struct adf_attachment_list *attachment;
622 mutex_lock(&dev->client_lock);
623 attachment = adf_attachment_find(&dev->attach_allowed, eng, intf);
624 mutex_unlock(&dev->client_lock);
626 return attachment != NULL;
628 EXPORT_SYMBOL(adf_device_attach_allowed);
630 * adf_device_attach - attach an overlay engine to an interface
632 * @dev: the parent device
633 * @eng: the overlay engine
634 * @intf: the interface
636 * Returns 0 on success, -%EINVAL if attaching @intf and @eng is not allowed,
637 * -%EALREADY if @intf and @eng are already attached, or -errno on any other
640 int adf_device_attach(struct adf_device *dev, struct adf_overlay_engine *eng,
641 struct adf_interface *intf)
644 struct adf_attachment_list *attachment = NULL;
646 ret = adf_attachment_validate(dev, eng, intf);
650 mutex_lock(&dev->client_lock);
652 if (dev->n_attached == ADF_MAX_ATTACHMENTS) {
657 if (!adf_attachment_find(&dev->attach_allowed, eng, intf)) {
662 if (adf_attachment_find(&dev->attached, eng, intf)) {
667 ret = adf_device_attach_op(dev, eng, intf);
671 attachment = kzalloc(sizeof(*attachment), GFP_KERNEL);
677 attachment->attachment.interface = intf;
678 attachment->attachment.overlay_engine = eng;
679 list_add_tail(&attachment->head, &dev->attached);
683 mutex_unlock(&dev->client_lock);
689 EXPORT_SYMBOL(adf_device_attach);
692 * adf_device_detach - detach an overlay engine from an interface
694 * @dev: the parent device
695 * @eng: the overlay engine
696 * @intf: the interface
698 * Returns 0 on success, -%EINVAL if @intf and @eng are not attached,
699 * or -errno on any other failure.
701 int adf_device_detach(struct adf_device *dev, struct adf_overlay_engine *eng,
702 struct adf_interface *intf)
705 struct adf_attachment_list *attachment;
707 ret = adf_attachment_validate(dev, eng, intf);
711 mutex_lock(&dev->client_lock);
713 attachment = adf_attachment_find(&dev->attached, eng, intf);
719 ret = adf_device_detach_op(dev, eng, intf);
723 adf_attachment_free(attachment);
726 mutex_unlock(&dev->client_lock);
729 EXPORT_SYMBOL(adf_device_detach);
732 * adf_interface_simple_buffer_alloc - allocate a simple buffer
734 * @intf: target interface
735 * @w: width in pixels
736 * @h: height in pixels
737 * @format: format fourcc
738 * @dma_buf: returns the allocated buffer
739 * @offset: returns the byte offset of the allocated buffer's first pixel
740 * @pitch: returns the allocated buffer's pitch
742 * See &struct adf_simple_buffer_alloc for a description of simple buffers and
745 * Returns 0 on success or -errno on failure.
747 int adf_interface_simple_buffer_alloc(struct adf_interface *intf, u16 w, u16 h,
748 u32 format, struct dma_buf **dma_buf, u32 *offset, u32 *pitch)
750 if (!intf->ops || !intf->ops->alloc_simple_buffer)
753 if (!adf_format_is_rgb(format))
756 return intf->ops->alloc_simple_buffer(intf, w, h, format, dma_buf,
759 EXPORT_SYMBOL(adf_interface_simple_buffer_alloc);
762 * adf_interface_simple_post - flip to a single buffer
764 * @intf: interface targeted by the flip
765 * @buf: buffer to display
767 * adf_interface_simple_post() can be used generically for simple display
768 * configurations, since the client does not need to provide any driver-private
769 * configuration data.
771 * adf_interface_simple_post() has the same copying semantics as
774 * On success, returns a sync fence which signals when the buffer is removed
775 * from the screen. On failure, returns ERR_PTR(-errno).
777 struct sync_fence *adf_interface_simple_post(struct adf_interface *intf,
778 struct adf_buffer *buf)
780 size_t custom_data_size = 0;
781 void *custom_data = NULL;
782 struct sync_fence *ret;
784 if (intf->ops && intf->ops->describe_simple_post) {
787 custom_data = kzalloc(ADF_MAX_CUSTOM_DATA_SIZE, GFP_KERNEL);
789 ret = ERR_PTR(-ENOMEM);
793 err = intf->ops->describe_simple_post(intf, buf, custom_data,
801 ret = adf_device_post(adf_interface_parent(intf), &intf, 1, buf, 1,
802 custom_data, custom_data_size);
807 EXPORT_SYMBOL(adf_interface_simple_post);