/*1* Copyright (c) 2016 Intel Corporation2*3* Permission to use, copy, modify, distribute, and sell this software and its4* documentation for any purpose is hereby granted without fee, provided that5* the above copyright notice appear in all copies and that both that copyright6* notice and this permission notice appear in supporting documentation, and7* that the name of the copyright holders not be used in advertising or8* publicity pertaining to distribution of the software without specific,9* written prior permission. The copyright holders make no representations10* about the suitability of this software for any purpose. It is provided "as11* is" without express or implied warranty.12*13* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,14* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO15* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR16* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,17* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER18* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE19* OF THIS SOFTWARE.20*/2122#include <linux/export.h>2324#include <drm/drm_bridge.h>25#include <drm/drm_device.h>26#include <drm/drm_drv.h>27#include <drm/drm_encoder.h>28#include <drm/drm_managed.h>29#include <drm/drm_print.h>3031#include "drm_crtc_internal.h"32#include "drm_internal.h"3334/**35* DOC: overview36*37* Encoders represent the connecting element between the CRTC (as the overall38* pixel pipeline, represented by &struct drm_crtc) and the connectors (as the39* generic sink entity, represented by &struct drm_connector). An encoder takes40* pixel data from a CRTC and converts it to a format suitable for any attached41* connector. Encoders are objects exposed to userspace, originally to allow42* userspace to infer cloning and connector/CRTC restrictions. Unfortunately43* almost all drivers get this wrong, making the uabi pretty much useless. On44* top of that the exposed restrictions are too simple for today's hardware, and45* the recommended way to infer restrictions is by using the46* DRM_MODE_ATOMIC_TEST_ONLY flag for the atomic IOCTL.47*48* Otherwise encoders aren't used in the uapi at all (any modeset request from49* userspace directly connects a connector with a CRTC), drivers are therefore50* free to use them however they wish. Modeset helper libraries make strong use51* of encoders to facilitate code sharing. But for more complex settings it is52* usually better to move shared code into a separate &drm_bridge. Compared to53* encoders, bridges also have the benefit of being purely an internal54* abstraction since they are not exposed to userspace at all.55*56* Encoders are initialized with drm_encoder_init() and cleaned up using57* drm_encoder_cleanup().58*/59static const struct drm_prop_enum_list drm_encoder_enum_list[] = {60{ DRM_MODE_ENCODER_NONE, "None" },61{ DRM_MODE_ENCODER_DAC, "DAC" },62{ DRM_MODE_ENCODER_TMDS, "TMDS" },63{ DRM_MODE_ENCODER_LVDS, "LVDS" },64{ DRM_MODE_ENCODER_TVDAC, "TV" },65{ DRM_MODE_ENCODER_VIRTUAL, "Virtual" },66{ DRM_MODE_ENCODER_DSI, "DSI" },67{ DRM_MODE_ENCODER_DPMST, "DP MST" },68{ DRM_MODE_ENCODER_DPI, "DPI" },69};7071int drm_encoder_register_all(struct drm_device *dev)72{73struct drm_encoder *encoder;74int ret = 0;7576drm_for_each_encoder(encoder, dev) {77drm_debugfs_encoder_add(encoder);7879if (encoder->funcs && encoder->funcs->late_register)80ret = encoder->funcs->late_register(encoder);81if (ret)82return ret;83}8485return 0;86}8788void drm_encoder_unregister_all(struct drm_device *dev)89{90struct drm_encoder *encoder;9192drm_for_each_encoder(encoder, dev) {93if (encoder->funcs && encoder->funcs->early_unregister)94encoder->funcs->early_unregister(encoder);95drm_debugfs_encoder_remove(encoder);96}97}9899__printf(5, 0)100static int __drm_encoder_init(struct drm_device *dev,101struct drm_encoder *encoder,102const struct drm_encoder_funcs *funcs,103int encoder_type, const char *name, va_list ap)104{105int ret;106107/* encoder index is used with 32bit bitmasks */108if (WARN_ON(dev->mode_config.num_encoder >= 32))109return -EINVAL;110111ret = drm_mode_object_add(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);112if (ret)113return ret;114115encoder->dev = dev;116encoder->encoder_type = encoder_type;117encoder->funcs = funcs;118if (name) {119encoder->name = kvasprintf(GFP_KERNEL, name, ap);120} else {121encoder->name = kasprintf(GFP_KERNEL, "%s-%d",122drm_encoder_enum_list[encoder_type].name,123encoder->base.id);124}125if (!encoder->name) {126ret = -ENOMEM;127goto out_put;128}129130INIT_LIST_HEAD(&encoder->bridge_chain);131list_add_tail(&encoder->head, &dev->mode_config.encoder_list);132encoder->index = dev->mode_config.num_encoder++;133134out_put:135if (ret)136drm_mode_object_unregister(dev, &encoder->base);137138return ret;139}140141/**142* drm_encoder_init - Init a preallocated encoder143* @dev: drm device144* @encoder: the encoder to init145* @funcs: callbacks for this encoder146* @encoder_type: user visible type of the encoder147* @name: printf style format string for the encoder name, or NULL for default name148*149* Initializes a preallocated encoder. Encoder should be subclassed as part of150* driver encoder objects. At driver unload time the driver's151* &drm_encoder_funcs.destroy hook should call drm_encoder_cleanup() and kfree()152* the encoder structure. The encoder structure should not be allocated with153* devm_kzalloc().154*155* Note: consider using drmm_encoder_alloc() or drmm_encoder_init()156* instead of drm_encoder_init() to let the DRM managed resource157* infrastructure take care of cleanup and deallocation.158*159* Returns:160* Zero on success, error code on failure.161*/162int drm_encoder_init(struct drm_device *dev,163struct drm_encoder *encoder,164const struct drm_encoder_funcs *funcs,165int encoder_type, const char *name, ...)166{167va_list ap;168int ret;169170WARN_ON(!funcs->destroy);171172va_start(ap, name);173ret = __drm_encoder_init(dev, encoder, funcs, encoder_type, name, ap);174va_end(ap);175176return ret;177}178EXPORT_SYMBOL(drm_encoder_init);179180/**181* drm_encoder_cleanup - cleans up an initialised encoder182* @encoder: encoder to cleanup183*184* Cleans up the encoder but doesn't free the object.185*/186void drm_encoder_cleanup(struct drm_encoder *encoder)187{188struct drm_device *dev = encoder->dev;189struct drm_bridge *bridge, *next;190191/* Note that the encoder_list is considered to be static; should we192* remove the drm_encoder at runtime we would have to decrement all193* the indices on the drm_encoder after us in the encoder_list.194*/195196list_for_each_entry_safe(bridge, next, &encoder->bridge_chain,197chain_node)198drm_bridge_detach(bridge);199200drm_mode_object_unregister(dev, &encoder->base);201kfree(encoder->name);202list_del(&encoder->head);203dev->mode_config.num_encoder--;204205memset(encoder, 0, sizeof(*encoder));206}207EXPORT_SYMBOL(drm_encoder_cleanup);208209static void drmm_encoder_alloc_release(struct drm_device *dev, void *ptr)210{211struct drm_encoder *encoder = ptr;212213if (WARN_ON(!encoder->dev))214return;215216drm_encoder_cleanup(encoder);217}218219__printf(5, 0)220static int __drmm_encoder_init(struct drm_device *dev,221struct drm_encoder *encoder,222const struct drm_encoder_funcs *funcs,223int encoder_type,224const char *name,225va_list args)226{227int ret;228229if (drm_WARN_ON(dev, funcs && funcs->destroy))230return -EINVAL;231232ret = __drm_encoder_init(dev, encoder, funcs, encoder_type, name, args);233if (ret)234return ret;235236ret = drmm_add_action_or_reset(dev, drmm_encoder_alloc_release, encoder);237if (ret)238return ret;239240return 0;241}242243void *__drmm_encoder_alloc(struct drm_device *dev, size_t size, size_t offset,244const struct drm_encoder_funcs *funcs,245int encoder_type, const char *name, ...)246{247void *container;248struct drm_encoder *encoder;249va_list ap;250int ret;251252container = drmm_kzalloc(dev, size, GFP_KERNEL);253if (!container)254return ERR_PTR(-ENOMEM);255256encoder = container + offset;257258va_start(ap, name);259ret = __drmm_encoder_init(dev, encoder, funcs, encoder_type, name, ap);260va_end(ap);261if (ret)262return ERR_PTR(ret);263264return container;265}266EXPORT_SYMBOL(__drmm_encoder_alloc);267268/**269* drmm_encoder_init - Initialize a preallocated encoder270* @dev: drm device271* @encoder: the encoder to init272* @funcs: callbacks for this encoder (optional)273* @encoder_type: user visible type of the encoder274* @name: printf style format string for the encoder name, or NULL for default name275*276* Initializes a preallocated encoder. Encoder should be subclassed as277* part of driver encoder objects. Cleanup is automatically handled278* through registering drm_encoder_cleanup() with drmm_add_action(). The279* encoder structure should be allocated with drmm_kzalloc().280*281* The @drm_encoder_funcs.destroy hook must be NULL.282*283* Returns:284* Zero on success, error code on failure.285*/286int drmm_encoder_init(struct drm_device *dev, struct drm_encoder *encoder,287const struct drm_encoder_funcs *funcs,288int encoder_type, const char *name, ...)289{290va_list ap;291int ret;292293va_start(ap, name);294ret = __drmm_encoder_init(dev, encoder, funcs, encoder_type, name, ap);295va_end(ap);296if (ret)297return ret;298299return 0;300}301EXPORT_SYMBOL(drmm_encoder_init);302303static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)304{305struct drm_connector *connector;306struct drm_device *dev = encoder->dev;307bool uses_atomic = false;308struct drm_connector_list_iter conn_iter;309310/* For atomic drivers only state objects are synchronously updated and311* protected by modeset locks, so check those first. */312drm_connector_list_iter_begin(dev, &conn_iter);313drm_for_each_connector_iter(connector, &conn_iter) {314if (!connector->state)315continue;316317uses_atomic = true;318319if (connector->state->best_encoder != encoder)320continue;321322drm_connector_list_iter_end(&conn_iter);323return connector->state->crtc;324}325drm_connector_list_iter_end(&conn_iter);326327/* Don't return stale data (e.g. pending async disable). */328if (uses_atomic)329return NULL;330331return encoder->crtc;332}333334int drm_mode_getencoder(struct drm_device *dev, void *data,335struct drm_file *file_priv)336{337struct drm_mode_get_encoder *enc_resp = data;338struct drm_encoder *encoder;339struct drm_crtc *crtc;340341if (!drm_core_check_feature(dev, DRIVER_MODESET))342return -EOPNOTSUPP;343344encoder = drm_encoder_find(dev, file_priv, enc_resp->encoder_id);345if (!encoder)346return -ENOENT;347348drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);349crtc = drm_encoder_get_crtc(encoder);350if (crtc && drm_lease_held(file_priv, crtc->base.id))351enc_resp->crtc_id = crtc->base.id;352else353enc_resp->crtc_id = 0;354drm_modeset_unlock(&dev->mode_config.connection_mutex);355356enc_resp->encoder_type = encoder->encoder_type;357enc_resp->encoder_id = encoder->base.id;358enc_resp->possible_crtcs = drm_lease_filter_crtcs(file_priv,359encoder->possible_crtcs);360enc_resp->possible_clones = encoder->possible_clones;361362return 0;363}364365366