public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3 0/3] drm: atmel-hlcdc: clut support
@ 2017-06-19  7:44 Peter Rosin
  2017-06-19  7:44 ` [PATCH v3 1/3] drm: atmel-hlcdc: add support for 8-bit color lookup table mode Peter Rosin
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: Peter Rosin @ 2017-06-19  7:44 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Rosin, Boris Brezillon, David Airlie, Daniel Vetter,
	Jani Nikula, Sean Paul, dri-devel

Hi!

This series adds support for an 8-bit clut mode in the atmel-hlcdc
driver.

I have now tested patch 1 with the below program (modeset.c
adapted from https://github.com/dvdhrm/docs/tree/master/drm-howto
to use an 8-bit mode).

Since v2 I have also cleared up why the first 16 entries of the clut
was not working right. It was of course my own damn fault, and the
fix was in atmel_hlcdc_layer_write_clut function which called the
...write_reg function which in turn added an extra offset of 16
registers...

Changes since v2:

- Fix mapping to the clut registers.

Changes since v1:

- Move the clut update from atmel_hlcdc_crtc_mode_valid to
  atmel_hlcdc_plane_atomic_update.
- Add default .gamma_set helper (drm_atomic_helper_legacy_gamma_set).
- Don't keep a spare copy of the clut, reuse gamma_store instead.
- Don't try to synchronize the legacy fb clut with the drm clut.

As I said in v2, I have not added any .clut_offset to the overlay2
layer of sama5d4, since the chip does not appear to have that layer.
I didn't do that to make it easier to work with the patch previously
sent to remove that layer, but I suspect bad things may happen to
sama5d4 users if they do not have that layer removed.

Cheers,
peda

modeset-pal.c (didn't update any comments, sorry)
----------------8<---------------
/*
 * modeset - DRM Modesetting Example
 *
 * Written 2012 by David Herrmann <dh.herrmann@googlemail.com>
 * Dedicated to the Public Domain.
 */

/*
 * DRM Modesetting Howto
 * This document describes the DRM modesetting API. Before we can use the DRM
 * API, we have to include xf86drm.h and xf86drmMode.h. Both are provided by
 * libdrm which every major distribution ships by default. It has no other
 * dependencies and is pretty small.
 *
 * Please ignore all forward-declarations of functions which are used later. I
 * reordered the functions so you can read this document from top to bottom. If
 * you reimplement it, you would probably reorder the functions to avoid all the
 * nasty forward declarations.
 *
 * For easier reading, we ignore all memory-allocation errors of malloc() and
 * friends here. However, we try to correctly handle all other kinds of errors
 * that may occur.
 *
 * All functions and global variables are prefixed with "modeset_*" in this
 * file. So it should be clear whether a function is a local helper or if it is
 * provided by some external library.
 */

#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <time.h>
#include <unistd.h>
#include <xf86drm.h>
#include <xf86drmMode.h>

struct modeset_dev;
static int modeset_find_crtc(int fd, drmModeRes *res, drmModeConnector *conn,
			     struct modeset_dev *dev);
static int modeset_create_fb(int fd, struct modeset_dev *dev);
static int modeset_setup_dev(int fd, drmModeRes *res, drmModeConnector *conn,
			     struct modeset_dev *dev);
static int modeset_open(int *out, const char *node);
static int modeset_prepare(int fd);
static void modeset_draw(int fd);
static void modeset_cleanup(int fd);

/*
 * When the linux kernel detects a graphics-card on your machine, it loads the
 * correct device driver (located in kernel-tree at ./drivers/gpu/drm/<xy>) and
 * provides two character-devices to control it. Udev (or whatever hotplugging
 * application you use) will create them as:
 *     /dev/dri/card0
 *     /dev/dri/controlID64
 * We only need the first one. You can hard-code this path into your application
 * like we do here, but it is recommended to use libudev with real hotplugging
 * and multi-seat support. However, this is beyond the scope of this document.
 * Also note that if you have multiple graphics-cards, there may also be
 * /dev/dri/card1, /dev/dri/card2, ...
 *
 * We simply use /dev/dri/card0 here but the user can specify another path on
 * the command line.
 *
 * modeset_open(out, node): This small helper function opens the DRM device
 * which is given as @node. The new fd is stored in @out on success. On failure,
 * a negative error code is returned.
 * After opening the file, we also check for the DRM_CAP_DUMB_BUFFER capability.
 * If the driver supports this capability, we can create simple memory-mapped
 * buffers without any driver-dependent code. As we want to avoid any radeon,
 * nvidia, intel, etc. specific code, we depend on DUMB_BUFFERs here.
 */

static int modeset_open(int *out, const char *node)
{
	int fd, ret;
	uint64_t has_dumb;

	fd = open(node, O_RDWR | O_CLOEXEC);
	if (fd < 0) {
		ret = -errno;
		fprintf(stderr, "cannot open '%s': %m\n", node);
		return ret;
	}

	if (drmGetCap(fd, DRM_CAP_DUMB_BUFFER, &has_dumb) < 0 ||
	    !has_dumb) {
		fprintf(stderr, "drm device '%s' does not support dumb buffers\n",
			node);
		close(fd);
		return -EOPNOTSUPP;
	}

	*out = fd;
	return 0;
}

/*
 * As a next step we need to find our available display devices. libdrm provides
 * a drmModeRes structure that contains all the needed information. We can
 * retrieve it via drmModeGetResources(fd) and free it via
 * drmModeFreeResources(res) again.
 *
 * A physical connector on your graphics card is called a "connector". You can
 * plug a monitor into it and control what is displayed. We are definitely
 * interested in what connectors are currently used, so we simply iterate
 * through the list of connectors and try to display a test-picture on each
 * available monitor.
 * However, this isn't as easy as it sounds. First, we need to check whether the
 * connector is actually used (a monitor is plugged in and turned on). Then we
 * need to find a CRTC that can control this connector. CRTCs are described
 * later on. After that we create a framebuffer object. If we have all this, we
 * can mmap() the framebuffer and draw a test-picture into it. Then we can tell
 * the DRM device to show the framebuffer on the given CRTC with the selected
 * connector.
 *
 * As we want to draw moving pictures on the framebuffer, we actually have to
 * remember all these settings. Therefore, we create one "struct modeset_dev"
 * object for each connector+crtc+framebuffer pair that we successfully
 * initialized and push it into the global device-list.
 *
 * Each field of this structure is described when it is first used. But as a
 * summary:
 * "struct modeset_dev" contains: {
 *  - @next: points to the next device in the single-linked list
 *
 *  - @width: width of our buffer object
 *  - @height: height of our buffer object
 *  - @stride: stride value of our buffer object
 *  - @size: size of the memory mapped buffer
 *  - @handle: a DRM handle to the buffer object that we can draw into
 *  - @map: pointer to the memory mapped buffer
 *
 *  - @mode: the display mode that we want to use
 *  - @fb: a framebuffer handle with our buffer object as scanout buffer
 *  - @conn: the connector ID that we want to use with this buffer
 *  - @crtc: the crtc ID that we want to use with this connector
 *  - @saved_crtc: the configuration of the crtc before we changed it. We use it
 *                 so we can restore the same mode when we exit.
 * }
 */

struct modeset_dev {
	struct modeset_dev *next;

	uint32_t width;
	uint32_t height;
	uint32_t stride;
	uint32_t size;
	uint32_t handle;
	uint8_t *map;

	drmModeModeInfo mode;
	uint32_t fb;
	uint32_t conn;
	uint32_t crtc;
	drmModeCrtc *saved_crtc;
};

static struct modeset_dev *modeset_list = NULL;

/*
 * So as next step we need to actually prepare all connectors that we find. We
 * do this in this little helper function:
 *
 * modeset_prepare(fd): This helper function takes the DRM fd as argument and
 * then simply retrieves the resource-info from the device. It then iterates
 * through all connectors and calls other helper functions to initialize this
 * connector (described later on).
 * If the initialization was successful, we simply add this object as new device
 * into the global modeset device list.
 *
 * The resource-structure contains a list of all connector-IDs. We use the
 * helper function drmModeGetConnector() to retrieve more information on each
 * connector. After we are done with it, we free it again with
 * drmModeFreeConnector().
 * Our helper modeset_setup_dev() returns -ENOENT if the connector is currently
 * unused and no monitor is plugged in. So we can ignore this connector.
 */

static int modeset_prepare(int fd)
{
	drmModeRes *res;
	drmModeConnector *conn;
	unsigned int i;
	struct modeset_dev *dev;
	int ret;

	/* retrieve resources */
	res = drmModeGetResources(fd);
	if (!res) {
		fprintf(stderr, "cannot retrieve DRM resources (%d): %m\n",
			errno);
		return -errno;
	}

	/* iterate all connectors */
	for (i = 0; i < res->count_connectors; ++i) {
		/* get information for each connector */
		conn = drmModeGetConnector(fd, res->connectors[i]);
		if (!conn) {
			fprintf(stderr, "cannot retrieve DRM connector %u:%u (%d): %m\n",
				i, res->connectors[i], errno);
			continue;
		}

		/* create a device structure */
		dev = malloc(sizeof(*dev));
		memset(dev, 0, sizeof(*dev));
		dev->conn = conn->connector_id;

		/* call helper function to prepare this connector */
		ret = modeset_setup_dev(fd, res, conn, dev);
		if (ret) {
			if (ret != -ENOENT) {
				errno = -ret;
				fprintf(stderr, "cannot setup device for connector %u:%u (%d): %m\n",
					i, res->connectors[i], errno);
			}
			free(dev);
			drmModeFreeConnector(conn);
			continue;
		}

		/* free connector data and link device into global list */
		drmModeFreeConnector(conn);
		dev->next = modeset_list;
		modeset_list = dev;
	}

	/* free resources again */
	drmModeFreeResources(res);
	return 0;
}

/*
 * Now we dig deeper into setting up a single connector. As described earlier,
 * we need to check several things first:
 *   * If the connector is currently unused, that is, no monitor is plugged in,
 *     then we can ignore it.
 *   * We have to find a suitable resolution and refresh-rate. All this is
 *     available in drmModeModeInfo structures saved for each crtc. We simply
 *     use the first mode that is available. This is always the mode with the
 *     highest resolution.
 *     A more sophisticated mode-selection should be done in real applications,
 *     though.
 *   * Then we need to find an CRTC that can drive this connector. A CRTC is an
 *     internal resource of each graphics-card. The number of CRTCs controls how
 *     many connectors can be controlled indepedently. That is, a graphics-cards
 *     may have more connectors than CRTCs, which means, not all monitors can be
 *     controlled independently.
 *     There is actually the possibility to control multiple connectors via a
 *     single CRTC if the monitors should display the same content. However, we
 *     do not make use of this here.
 *     So think of connectors as pipelines to the connected monitors and the
 *     CRTCs are the controllers that manage which data goes to which pipeline.
 *     If there are more pipelines than CRTCs, then we cannot control all of
 *     them at the same time.
 *   * We need to create a framebuffer for this connector. A framebuffer is a
 *     memory buffer that we can write XRGB32 data into. So we use this to
 *     render our graphics and then the CRTC can scan-out this data from the
 *     framebuffer onto the monitor.
 */

static int modeset_setup_dev(int fd, drmModeRes *res, drmModeConnector *conn,
			     struct modeset_dev *dev)
{
	int ret;

	/* check if a monitor is connected */
	if (conn->connection != DRM_MODE_CONNECTED) {
		fprintf(stderr, "ignoring unused connector %u\n",
			conn->connector_id);
		return -ENOENT;
	}

	/* check if there is at least one valid mode */
	if (conn->count_modes == 0) {
		fprintf(stderr, "no valid mode for connector %u\n",
			conn->connector_id);
		return -EFAULT;
	}

	/* copy the mode information into our device structure */
	memcpy(&dev->mode, &conn->modes[0], sizeof(dev->mode));
	dev->width = conn->modes[0].hdisplay;
	dev->height = conn->modes[0].vdisplay;
	fprintf(stderr, "mode for connector %u is %ux%u\n",
		conn->connector_id, dev->width, dev->height);

	/* find a crtc for this connector */
	ret = modeset_find_crtc(fd, res, conn, dev);
	if (ret) {
		fprintf(stderr, "no valid crtc for connector %u\n",
			conn->connector_id);
		return ret;
	}

	/* create a framebuffer for this CRTC */
	ret = modeset_create_fb(fd, dev);
	if (ret) {
		fprintf(stderr, "cannot create framebuffer for connector %u\n",
			conn->connector_id);
		return ret;
	}

	return 0;
}

/*
 * modeset_find_crtc(fd, res, conn, dev): This small helper tries to find a
 * suitable CRTC for the given connector. We have actually have to introduce one
 * more DRM object to make this more clear: Encoders.
 * Encoders help the CRTC to convert data from a framebuffer into the right
 * format that can be used for the chosen connector. We do not have to
 * understand any more of these conversions to make use of it. However, you must
 * know that each connector has a limited list of encoders that it can use. And
 * each encoder can only work with a limited list of CRTCs. So what we do is
 * trying each encoder that is available and looking for a CRTC that this
 * encoder can work with. If we find the first working combination, we are happy
 * and write it into the @dev structure.
 * But before iterating all available encoders, we first try the currently
 * active encoder+crtc on a connector to avoid a full modeset.
 *
 * However, before we can use a CRTC we must make sure that no other device,
 * that we setup previously, is already using this CRTC. Remember, we can only
 * drive one connector per CRTC! So we simply iterate through the "modeset_list"
 * of previously setup devices and check that this CRTC wasn't used before.
 * Otherwise, we continue with the next CRTC/Encoder combination.
 */

static int modeset_find_crtc(int fd, drmModeRes *res, drmModeConnector *conn,
			     struct modeset_dev *dev)
{
	drmModeEncoder *enc;
	unsigned int i, j;
	int32_t crtc;
	struct modeset_dev *iter;

	/* first try the currently conected encoder+crtc */
	if (conn->encoder_id)
		enc = drmModeGetEncoder(fd, conn->encoder_id);
	else
		enc = NULL;

	if (enc) {
		if (enc->crtc_id) {
			crtc = enc->crtc_id;
			for (iter = modeset_list; iter; iter = iter->next) {
				if (iter->crtc == crtc) {
					crtc = -1;
					break;
				}
			}

			if (crtc >= 0) {
				drmModeFreeEncoder(enc);
				dev->crtc = crtc;
				return 0;
			}
		}

		drmModeFreeEncoder(enc);
	}

	/* If the connector is not currently bound to an encoder or if the
	 * encoder+crtc is already used by another connector (actually unlikely
	 * but lets be safe), iterate all other available encoders to find a
	 * matching CRTC. */
	for (i = 0; i < conn->count_encoders; ++i) {
		enc = drmModeGetEncoder(fd, conn->encoders[i]);
		if (!enc) {
			fprintf(stderr, "cannot retrieve encoder %u:%u (%d): %m\n",
				i, conn->encoders[i], errno);
			continue;
		}

		/* iterate all global CRTCs */
		for (j = 0; j < res->count_crtcs; ++j) {
			/* check whether this CRTC works with the encoder */
			if (!(enc->possible_crtcs & (1 << j)))
				continue;

			/* check that no other device already uses this CRTC */
			crtc = res->crtcs[j];
			for (iter = modeset_list; iter; iter = iter->next) {
				if (iter->crtc == crtc) {
					crtc = -1;
					break;
				}
			}

			/* we have found a CRTC, so save it and return */
			if (crtc >= 0) {
				drmModeFreeEncoder(enc);
				dev->crtc = crtc;
				return 0;
			}
		}

		drmModeFreeEncoder(enc);
	}

	fprintf(stderr, "cannot find suitable CRTC for connector %u\n",
		conn->connector_id);
	return -ENOENT;
}

/*
 * modeset_create_fb(fd, dev): After we have found a crtc+connector+mode
 * combination, we need to actually create a suitable framebuffer that we can
 * use with it. There are actually two ways to do that:
 *   * We can create a so called "dumb buffer". This is a buffer that we can
 *     memory-map via mmap() and every driver supports this. We can use it for
 *     unaccelerated software rendering on the CPU.
 *   * We can use libgbm to create buffers available for hardware-acceleration.
 *     libgbm is an abstraction layer that creates these buffers for each
 *     available DRM driver. As there is no generic API for this, each driver
 *     provides its own way to create these buffers.
 *     We can then use such buffers to create OpenGL contexts with the mesa3D
 *     library.
 * We use the first solution here as it is much simpler and doesn't require any
 * external libraries. However, if you want to use hardware-acceleration via
 * OpenGL, it is actually pretty easy to create such buffers with libgbm and
 * libEGL. But this is beyond the scope of this document.
 *
 * So what we do is requesting a new dumb-buffer from the driver. We specify the
 * same size as the current mode that we selected for the connector.
 * Then we request the driver to prepare this buffer for memory mapping. After
 * that we perform the actual mmap() call. So we can now access the framebuffer
 * memory directly via the dev->map memory map.
 */

static int modeset_create_fb(int fd, struct modeset_dev *dev)
{
	struct drm_mode_create_dumb creq;
	struct drm_mode_destroy_dumb dreq;
	struct drm_mode_map_dumb mreq;
	int ret;

	/* create dumb buffer */
	memset(&creq, 0, sizeof(creq));
	creq.width = dev->width;
	creq.height = dev->height;
	creq.bpp = 8;
	ret = drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &creq);
	if (ret < 0) {
		fprintf(stderr, "cannot create dumb buffer (%d): %m\n",
			errno);
		return -errno;
	}
	dev->stride = creq.pitch;
	dev->size = creq.size;
	dev->handle = creq.handle;

	/* create framebuffer object for the dumb-buffer */
	ret = drmModeAddFB(fd, dev->width, dev->height, 8, 8, dev->stride,
			   dev->handle, &dev->fb);
	if (ret) {
		fprintf(stderr, "cannot create framebuffer (%d): %m\n",
			errno);
		ret = -errno;
		goto err_destroy;
	}

	/* prepare buffer for memory mapping */
	memset(&mreq, 0, sizeof(mreq));
	mreq.handle = dev->handle;
	ret = drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &mreq);
	if (ret) {
		fprintf(stderr, "cannot map dumb buffer (%d): %m\n",
			errno);
		ret = -errno;
		goto err_fb;
	}

	/* perform actual memory mapping */
	dev->map = mmap(0, dev->size, PROT_READ | PROT_WRITE, MAP_SHARED,
		        fd, mreq.offset);
	if (dev->map == MAP_FAILED) {
		fprintf(stderr, "cannot mmap dumb buffer (%d): %m\n",
			errno);
		ret = -errno;
		goto err_fb;
	}

	/* clear the framebuffer to 0 */
	memset(dev->map, 0, dev->size);

	return 0;

err_fb:
	drmModeRmFB(fd, dev->fb);
err_destroy:
	memset(&dreq, 0, sizeof(dreq));
	dreq.handle = dev->handle;
	drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);
	return ret;
}

/*
 * Finally! We have a connector with a suitable CRTC. We know which mode we want
 * to use and we have a framebuffer of the correct size that we can write to.
 * There is nothing special left to do. We only have to program the CRTC to
 * connect each new framebuffer to each selected connector for each combination
 * that we saved in the global modeset_list.
 * This is done with a call to drmModeSetCrtc().
 *
 * So we are ready for our main() function. First we check whether the user
 * specified a DRM device on the command line, otherwise we use the default
 * /dev/dri/card0. Then we open the device via modeset_open(). modeset_prepare()
 * prepares all connectors and we can loop over "modeset_list" and call
 * drmModeSetCrtc() on every CRTC/connector combination.
 *
 * But printing empty black pages is boring so we have another helper function
 * modeset_draw() that draws some colors into the framebuffer for 5 seconds and
 * then returns. And then we have all the cleanup functions which correctly free
 * all devices again after we used them. All these functions are described below
 * the main() function.
 *
 * As a side note: drmModeSetCrtc() actually takes a list of connectors that we
 * want to control with this CRTC. We pass only one connector, though. As
 * explained earlier, if we used multiple connectors, then all connectors would
 * have the same controlling framebuffer so the output would be cloned. This is
 * most often not what you want so we avoid explaining this feature here.
 * Furthermore, all connectors will have to run with the same mode, which is
 * also often not guaranteed. So instead, we only use one connector per CRTC.
 *
 * Before calling drmModeSetCrtc() we also save the current CRTC configuration.
 * This is used in modeset_cleanup() to restore the CRTC to the same mode as was
 * before we changed it.
 * If we don't do this, the screen will stay blank after we exit until another
 * application performs modesetting itself.
 */

int main(int argc, char **argv)
{
	int ret, fd;
	const char *card;
	struct modeset_dev *iter;

	/* check which DRM device to open */
	if (argc > 1)
		card = argv[1];
	else
		card = "/dev/dri/card0";

	fprintf(stderr, "using card '%s'\n", card);

	/* open the DRM device */
	ret = modeset_open(&fd, card);
	if (ret)
		goto out_return;

	/* prepare all connectors and CRTCs */
	ret = modeset_prepare(fd);
	if (ret)
		goto out_close;

	/* perform actual modesetting on each found connector+CRTC */
	for (iter = modeset_list; iter; iter = iter->next) {
		iter->saved_crtc = drmModeGetCrtc(fd, iter->crtc);
		ret = drmModeSetCrtc(fd, iter->crtc, iter->fb, 0, 0,
				     &iter->conn, 1, &iter->mode);
		if (ret)
			fprintf(stderr, "cannot set CRTC for connector %u (%d): %m\n",
				iter->conn, errno);
	}

	/* draw some colors for 5seconds */
	modeset_draw(fd);

	/* cleanup everything */
	modeset_cleanup(fd);

	ret = 0;

out_close:
	close(fd);
out_return:
	if (ret) {
		errno = -ret;
		fprintf(stderr, "modeset failed with error %d: %m\n", errno);
	} else {
		fprintf(stderr, "exiting\n");
	}
	return ret;
}

/*
 * A short helper function to compute a changing color value. No need to
 * understand it.
 */

static uint8_t next_color(bool *up, uint8_t cur, unsigned int mod)
{
	uint8_t next;

	next = cur + (*up ? 1 : -1) * (rand() % mod);
	if ((*up && next < cur) || (!*up && next > cur)) {
		*up = !*up;
		next = cur;
	}

	return next;
}

static void crtc_lut(int fd, struct modeset_dev *dev, int p)
{
	struct drm_mode_crtc_lut clut;
	uint16_t r[256];
	uint16_t g[256];
	uint16_t b[256];
	int ret;
	int i;

	/* prepare buffer for memory mapping */
	memset(&clut, 0, sizeof(clut));
	clut.crtc_id = dev->crtc;
	clut.gamma_size = 256;
	clut.red = (uint64_t)r;
	clut.green = (uint64_t)g;
	clut.blue = (uint64_t)b;

	for (i = 0; i < 256; ++i) {
		r[i] = ((p + 2 * i) & 255) * 257;
		g[i] = ((p + 3 * i) & 255) * 257;
		b[i] = ((p + 5 * i) & 255) * 257;
	}
	ret = drmIoctl(fd, DRM_IOCTL_MODE_SETGAMMA, &clut);
	if (ret)
		fprintf(stderr, "cannot set gamma lut (%d): %m\n",
			errno);
}

/*
 * modeset_draw(): This draws a solid color into all configured framebuffers.
 * Every 100ms the color changes to a slightly different color so we get some
 * kind of smoothly changing color-gradient.
 *
 * The color calculation can be ignored as it is pretty boring. So the
 * interesting stuff is iterating over "modeset_list" and then through all lines
 * and width. We then set each pixel individually to the current color.
 *
 * We do this 50 times as we sleep 100ms after each redraw round. This makes
 * 50*100ms = 5000ms = 5s so it takes about 5seconds to finish this loop.
 *
 * Please note that we draw directly into the framebuffer. This means that you
 * will see flickering as the monitor might refresh while we redraw the screen.
 * To avoid this you would need to use two framebuffers and a call to
 * drmModeSetCrtc() to switch between both buffers.
 * You can also use drmModePageFlip() to do a vsync'ed pageflip. But this is
 * beyond the scope of this document.
 */

static void modeset_draw(int fd)
{
	uint8_t p = 0;
	unsigned int i, j, k;
	struct modeset_dev *iter;

	for (iter = modeset_list; iter; iter = iter->next) {
		for (k = 0; k < iter->width; ++k) {
			for (j = 0; j < iter->height / 3; ++j) {
				iter->map[iter->stride * j + k] =
					k * 256 / iter->width;
			}
			for (; j < iter->height; ++j)
				iter->map[iter->stride * j + k] = 26;
		}
	}

	for (i = 0; i < 50; ++i, ++p) {
		for (iter = modeset_list; iter; iter = iter->next)
			crtc_lut(fd, iter, p);

		usleep(100000);
	}
}

/*
 * modeset_cleanup(fd): This cleans up all the devices we created during
 * modeset_prepare(). It resets the CRTCs to their saved states and deallocates
 * all memory.
 * It should be pretty obvious how all of this works.
 */

static void modeset_cleanup(int fd)
{
	struct modeset_dev *iter;
	struct drm_mode_destroy_dumb dreq;

	while (modeset_list) {
		/* remove from global list */
		iter = modeset_list;
		modeset_list = iter->next;

		/* restore saved CRTC configuration */
		drmModeSetCrtc(fd,
			       iter->saved_crtc->crtc_id,
			       iter->saved_crtc->buffer_id,
			       iter->saved_crtc->x,
			       iter->saved_crtc->y,
			       &iter->conn,
			       1,
			       &iter->saved_crtc->mode);
		drmModeFreeCrtc(iter->saved_crtc);

		/* unmap buffer */
		munmap(iter->map, iter->size);

		/* delete framebuffer */
		drmModeRmFB(fd, iter->fb);

		/* delete dumb buffer */
		memset(&dreq, 0, sizeof(dreq));
		dreq.handle = iter->handle;
		drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);

		/* free allocated memory */
		free(iter);
	}
}

/*
 * I hope this was a short but easy overview of the DRM modesetting API. The DRM
 * API offers much more capabilities including:
 *  - double-buffering or tripple-buffering (or whatever you want)
 *  - vsync'ed page-flips
 *  - hardware-accelerated rendering (for example via OpenGL)
 *  - output cloning
 *  - graphics-clients plus authentication
 *  - DRM planes/overlays/sprites
 *  - ...
 * If you are interested in these topics, I can currently only redirect you to
 * existing implementations, including:
 *  - plymouth (which uses dumb-buffers like this example; very easy to understand)
 *  - kmscon (which uses libuterm to do this)
 *  - wayland (very sophisticated DRM renderer; hard to understand fully as it
 *             uses more complicated techniques like DRM planes)
 *  - xserver (very hard to understand as it is split across many files/projects)
 *
 * But understanding how modesetting (as described in this document) works, is
 * essential to understand all further DRM topics.
 *
 * Any feedback is welcome. Feel free to use this code freely for your own
 * documentation or projects.
 *
 *  - Hosted on http://github.com/dvdhrm/docs
 *  - Written by David Herrmann <dh.herrmann@googlemail.com>
 */
----------------8<---------------

Peter Rosin (3):
  drm: atmel-hlcdc: add support for 8-bit color lookup table mode
  drm/fb-cma-helper: expose more of fb cma guts
  drm: atmel-hlcdc: add clut support for legacy fbdev

 drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_crtc.c  | 58 +++++++++++++++++++++++++
 drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_dc.c    | 25 ++++++++++-
 drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_dc.h    | 20 +++++++++
 drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_plane.c | 29 +++++++++++++
 drivers/gpu/drm/drm_fb_cma_helper.c             | 55 ++++++++++++++++++-----
 include/drm/drm_fb_cma_helper.h                 |  8 +++-
 6 files changed, 182 insertions(+), 13 deletions(-)

-- 
2.1.4

^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH v3 1/3] drm: atmel-hlcdc: add support for 8-bit color lookup table mode
  2017-06-19  7:44 [PATCH v3 0/3] drm: atmel-hlcdc: clut support Peter Rosin
@ 2017-06-19  7:44 ` Peter Rosin
  2017-06-19  7:44 ` [PATCH v3 2/3] drm/fb-cma-helper: expose more of fb cma guts Peter Rosin
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Peter Rosin @ 2017-06-19  7:44 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Rosin, Boris Brezillon, David Airlie, Daniel Vetter,
	Jani Nikula, Sean Paul, dri-devel

All layers of all supported chips support this, the only variable is the
base address of the lookup table in the register map.

Signed-off-by: Peter Rosin <peda@axentia.se>
---
 drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_crtc.c  |  5 +++++
 drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_dc.c    | 13 +++++++++++
 drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_dc.h    | 16 ++++++++++++++
 drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_plane.c | 29 +++++++++++++++++++++++++
 4 files changed, 63 insertions(+)

diff --git a/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_crtc.c b/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_crtc.c
index 5348985..694adcc 100644
--- a/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_crtc.c
+++ b/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_crtc.c
@@ -429,6 +429,8 @@ static const struct drm_crtc_funcs atmel_hlcdc_crtc_funcs = {
 	.atomic_destroy_state = atmel_hlcdc_crtc_destroy_state,
 	.enable_vblank = atmel_hlcdc_crtc_enable_vblank,
 	.disable_vblank = atmel_hlcdc_crtc_disable_vblank,
+	.set_property = drm_atomic_helper_crtc_set_property,
+	.gamma_set = drm_atomic_helper_legacy_gamma_set,
 };
 
 int atmel_hlcdc_crtc_create(struct drm_device *dev)
@@ -484,6 +486,9 @@ int atmel_hlcdc_crtc_create(struct drm_device *dev)
 	drm_crtc_helper_add(&crtc->base, &lcdc_crtc_helper_funcs);
 	drm_crtc_vblank_reset(&crtc->base);
 
+	drm_mode_crtc_set_gamma_size(&crtc->base, ATMEL_HLCDC_CLUT_SIZE);
+	drm_crtc_enable_color_mgmt(&crtc->base, 0, false, ATMEL_HLCDC_CLUT_SIZE);
+
 	dc->crtc = &crtc->base;
 
 	return 0;
diff --git a/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_dc.c b/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_dc.c
index 30dbffd..4f6ef07 100644
--- a/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_dc.c
+++ b/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_dc.c
@@ -42,6 +42,7 @@ static const struct atmel_hlcdc_layer_desc atmel_hlcdc_at91sam9n12_layers[] = {
 			.default_color = 3,
 			.general_config = 4,
 		},
+		.clut_offset = 0x400,
 	},
 };
 
@@ -73,6 +74,7 @@ static const struct atmel_hlcdc_layer_desc atmel_hlcdc_at91sam9x5_layers[] = {
 			.disc_pos = 5,
 			.disc_size = 6,
 		},
+		.clut_offset = 0x400,
 	},
 	{
 		.name = "overlay1",
@@ -91,6 +93,7 @@ static const struct atmel_hlcdc_layer_desc atmel_hlcdc_at91sam9x5_layers[] = {
 			.chroma_key_mask = 8,
 			.general_config = 9,
 		},
+		.clut_offset = 0x800,
 	},
 	{
 		.name = "high-end-overlay",
@@ -112,6 +115,7 @@ static const struct atmel_hlcdc_layer_desc atmel_hlcdc_at91sam9x5_layers[] = {
 			.scaler_config = 13,
 			.csc = 14,
 		},
+		.clut_offset = 0x1000,
 	},
 	{
 		.name = "cursor",
@@ -131,6 +135,7 @@ static const struct atmel_hlcdc_layer_desc atmel_hlcdc_at91sam9x5_layers[] = {
 			.chroma_key_mask = 8,
 			.general_config = 9,
 		},
+		.clut_offset = 0x1400,
 	},
 };
 
@@ -162,6 +167,7 @@ static const struct atmel_hlcdc_layer_desc atmel_hlcdc_sama5d3_layers[] = {
 			.disc_pos = 5,
 			.disc_size = 6,
 		},
+		.clut_offset = 0x600,
 	},
 	{
 		.name = "overlay1",
@@ -180,6 +186,7 @@ static const struct atmel_hlcdc_layer_desc atmel_hlcdc_sama5d3_layers[] = {
 			.chroma_key_mask = 8,
 			.general_config = 9,
 		},
+		.clut_offset = 0xa00,
 	},
 	{
 		.name = "overlay2",
@@ -198,6 +205,7 @@ static const struct atmel_hlcdc_layer_desc atmel_hlcdc_sama5d3_layers[] = {
 			.chroma_key_mask = 8,
 			.general_config = 9,
 		},
+		.clut_offset = 0xe00,
 	},
 	{
 		.name = "high-end-overlay",
@@ -223,6 +231,7 @@ static const struct atmel_hlcdc_layer_desc atmel_hlcdc_sama5d3_layers[] = {
 			},
 			.csc = 14,
 		},
+		.clut_offset = 0x1200,
 	},
 	{
 		.name = "cursor",
@@ -244,6 +253,7 @@ static const struct atmel_hlcdc_layer_desc atmel_hlcdc_sama5d3_layers[] = {
 			.general_config = 9,
 			.scaler_config = 13,
 		},
+		.clut_offset = 0x1600,
 	},
 };
 
@@ -275,6 +285,7 @@ static const struct atmel_hlcdc_layer_desc atmel_hlcdc_sama5d4_layers[] = {
 			.disc_pos = 5,
 			.disc_size = 6,
 		},
+		.clut_offset = 0x600,
 	},
 	{
 		.name = "overlay1",
@@ -293,6 +304,7 @@ static const struct atmel_hlcdc_layer_desc atmel_hlcdc_sama5d4_layers[] = {
 			.chroma_key_mask = 8,
 			.general_config = 9,
 		},
+		.clut_offset = 0xa00,
 	},
 	{
 		.name = "overlay2",
@@ -336,6 +348,7 @@ static const struct atmel_hlcdc_layer_desc atmel_hlcdc_sama5d4_layers[] = {
 			},
 			.csc = 14,
 		},
+		.clut_offset = 0x1200,
 	},
 };
 
diff --git a/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_dc.h b/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_dc.h
index b0596a8..4237b04 100644
--- a/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_dc.h
+++ b/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_dc.h
@@ -88,6 +88,11 @@
 #define ATMEL_HLCDC_YUV422SWP			BIT(17)
 #define ATMEL_HLCDC_DSCALEOPT			BIT(20)
 
+#define ATMEL_HLCDC_C1_MODE			ATMEL_HLCDC_CLUT_MODE(0)
+#define ATMEL_HLCDC_C2_MODE			ATMEL_HLCDC_CLUT_MODE(1)
+#define ATMEL_HLCDC_C4_MODE			ATMEL_HLCDC_CLUT_MODE(2)
+#define ATMEL_HLCDC_C8_MODE			ATMEL_HLCDC_CLUT_MODE(3)
+
 #define ATMEL_HLCDC_XRGB4444_MODE		ATMEL_HLCDC_RGB_MODE(0)
 #define ATMEL_HLCDC_ARGB4444_MODE		ATMEL_HLCDC_RGB_MODE(1)
 #define ATMEL_HLCDC_RGBA4444_MODE		ATMEL_HLCDC_RGB_MODE(2)
@@ -142,6 +147,8 @@
 #define ATMEL_HLCDC_DMA_CHANNEL_DSCR_DONE	BIT(2)
 #define ATMEL_HLCDC_DMA_CHANNEL_DSCR_OVERRUN	BIT(3)
 
+#define ATMEL_HLCDC_CLUT_SIZE			256
+
 #define ATMEL_HLCDC_MAX_LAYERS			6
 
 /**
@@ -259,6 +266,7 @@ struct atmel_hlcdc_layer_desc {
 	int id;
 	int regs_offset;
 	int cfgs_offset;
+	int clut_offset;
 	struct atmel_hlcdc_formats *formats;
 	struct atmel_hlcdc_layer_cfg_layout layout;
 	int max_width;
@@ -414,6 +422,14 @@ static inline u32 atmel_hlcdc_layer_read_cfg(struct atmel_hlcdc_layer *layer,
 					  (cfgid * sizeof(u32)));
 }
 
+static inline void atmel_hlcdc_layer_write_clut(struct atmel_hlcdc_layer *layer,
+						unsigned int c, u32 val)
+{
+	regmap_write(layer->regmap,
+		     layer->desc->clut_offset + c * sizeof(u32),
+		     val);
+}
+
 static inline void atmel_hlcdc_layer_init(struct atmel_hlcdc_layer *layer,
 				const struct atmel_hlcdc_layer_desc *desc,
 				struct regmap *regmap)
diff --git a/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_plane.c b/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_plane.c
index 1124200..b5bd9b0 100644
--- a/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_plane.c
+++ b/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_plane.c
@@ -83,6 +83,7 @@ drm_plane_state_to_atmel_hlcdc_plane_state(struct drm_plane_state *s)
 #define SUBPIXEL_MASK			0xffff
 
 static uint32_t rgb_formats[] = {
+	DRM_FORMAT_C8,
 	DRM_FORMAT_XRGB4444,
 	DRM_FORMAT_ARGB4444,
 	DRM_FORMAT_RGBA4444,
@@ -100,6 +101,7 @@ struct atmel_hlcdc_formats atmel_hlcdc_plane_rgb_formats = {
 };
 
 static uint32_t rgb_and_yuv_formats[] = {
+	DRM_FORMAT_C8,
 	DRM_FORMAT_XRGB4444,
 	DRM_FORMAT_ARGB4444,
 	DRM_FORMAT_RGBA4444,
@@ -128,6 +130,9 @@ struct atmel_hlcdc_formats atmel_hlcdc_plane_rgb_and_yuv_formats = {
 static int atmel_hlcdc_format_to_plane_mode(u32 format, u32 *mode)
 {
 	switch (format) {
+	case DRM_FORMAT_C8:
+		*mode = ATMEL_HLCDC_C8_MODE;
+		break;
 	case DRM_FORMAT_XRGB4444:
 		*mode = ATMEL_HLCDC_XRGB4444_MODE;
 		break;
@@ -424,6 +429,29 @@ static void atmel_hlcdc_plane_update_format(struct atmel_hlcdc_plane *plane,
 				    ATMEL_HLCDC_LAYER_FORMAT_CFG, cfg);
 }
 
+static void atmel_hlcdc_plane_update_clut(struct atmel_hlcdc_plane *plane)
+{
+	struct drm_crtc *crtc = plane->base.crtc;
+	struct drm_color_lut *lut;
+	int idx;
+
+	if (!crtc || !crtc->state)
+		return;
+
+	if (!crtc->state->color_mgmt_changed || !crtc->state->gamma_lut)
+		return;
+
+	lut = (struct drm_color_lut *)crtc->state->gamma_lut->data;
+
+	for (idx = 0; idx < ATMEL_HLCDC_CLUT_SIZE; idx++, lut++) {
+		u32 val = ((lut->red << 8) & 0xff0000) |
+			(lut->green & 0xff00) |
+			(lut->blue >> 8);
+
+		atmel_hlcdc_layer_write_clut(&plane->layer, idx, val);
+	}
+}
+
 static void atmel_hlcdc_plane_update_buffers(struct atmel_hlcdc_plane *plane,
 					struct atmel_hlcdc_plane_state *state)
 {
@@ -768,6 +796,7 @@ static void atmel_hlcdc_plane_atomic_update(struct drm_plane *p,
 	atmel_hlcdc_plane_update_pos_and_size(plane, state);
 	atmel_hlcdc_plane_update_general_settings(plane, state);
 	atmel_hlcdc_plane_update_format(plane, state);
+	atmel_hlcdc_plane_update_clut(plane);
 	atmel_hlcdc_plane_update_buffers(plane, state);
 	atmel_hlcdc_plane_update_disc_area(plane, state);
 
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v3 2/3] drm/fb-cma-helper: expose more of fb cma guts
  2017-06-19  7:44 [PATCH v3 0/3] drm: atmel-hlcdc: clut support Peter Rosin
  2017-06-19  7:44 ` [PATCH v3 1/3] drm: atmel-hlcdc: add support for 8-bit color lookup table mode Peter Rosin
@ 2017-06-19  7:44 ` Peter Rosin
  2017-06-19 20:41   ` Boris Brezillon
  2017-06-19  7:44 ` [PATCH v3 3/3] drm: atmel-hlcdc: add clut support for legacy fbdev Peter Rosin
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 7+ messages in thread
From: Peter Rosin @ 2017-06-19  7:44 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Rosin, Boris Brezillon, David Airlie, Daniel Vetter,
	Jani Nikula, Sean Paul, dri-devel

DRM drivers supporting clut may want a convenient way to only use
non-default .gamma_set and .gamma_get ops in the drm_fb_helper_funcs
in order to avoid the following

	/*
	 * The driver really shouldn't advertise pseudo/directcolor
	 * visuals if it can't deal with the palette.
	 */
	if (WARN_ON(!fb_helper->funcs->gamma_set ||
		    !fb_helper->funcs->gamma_get))
		return -EINVAL;

warning in drm_fb_helper.c:setcolreg().

Signed-off-by: Peter Rosin <peda@axentia.se>
---
 drivers/gpu/drm/drm_fb_cma_helper.c | 55 ++++++++++++++++++++++++++++++-------
 include/drm/drm_fb_cma_helper.h     |  8 +++++-
 2 files changed, 52 insertions(+), 11 deletions(-)

diff --git a/drivers/gpu/drm/drm_fb_cma_helper.c b/drivers/gpu/drm/drm_fb_cma_helper.c
index 53f9bdf..ef96227 100644
--- a/drivers/gpu/drm/drm_fb_cma_helper.c
+++ b/drivers/gpu/drm/drm_fb_cma_helper.c
@@ -426,7 +426,12 @@ static void drm_fbdev_cma_defio_fini(struct fb_info *fbi)
 	kfree(fbi->fbops);
 }
 
-static int
+/**
+ * drm_fbdev_cma_create() - Default fb_probe() function for fb_cma_helper_funcs
+ * @helper: The fb_helper to create a cma for
+ * @sizes: The fbdev sizes
+ */
+int
 drm_fbdev_cma_create(struct drm_fb_helper *helper,
 	struct drm_fb_helper_surface_size *sizes)
 {
@@ -507,23 +512,28 @@ drm_fbdev_cma_create(struct drm_fb_helper *helper,
 	drm_gem_object_put_unlocked(&obj->base);
 	return ret;
 }
+EXPORT_SYMBOL_GPL(drm_fbdev_cma_create);
 
 static const struct drm_fb_helper_funcs drm_fb_cma_helper_funcs = {
 	.fb_probe = drm_fbdev_cma_create,
 };
 
 /**
- * drm_fbdev_cma_init_with_funcs() - Allocate and initializes a drm_fbdev_cma struct
+ * drm_fbdev_cma_init_with_funcs2() - Allocate and initializes a drm_fbdev_cma struct
  * @dev: DRM device
  * @preferred_bpp: Preferred bits per pixel for the device
  * @max_conn_count: Maximum number of connectors
- * @funcs: fb helper functions, in particular a custom dirty() callback
+ * @framebuffer_funcs: framebuffer functions, in particular a custom dirty() callback
+ * @fb_helper_funcs: fb helper functions, in particular custom gamma_set() and gamma_get() callbacks
+ *
+ * If framebuffer_funcs or fb_helper_funcs are NULL, default functions are used.
  *
  * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
  */
-struct drm_fbdev_cma *drm_fbdev_cma_init_with_funcs(struct drm_device *dev,
+struct drm_fbdev_cma *drm_fbdev_cma_init_with_funcs2(struct drm_device *dev,
 	unsigned int preferred_bpp, unsigned int max_conn_count,
-	const struct drm_framebuffer_funcs *funcs)
+	const struct drm_framebuffer_funcs *framebuffer_funcs,
+	const struct drm_fb_helper_funcs *fb_helper_funcs)
 {
 	struct drm_fbdev_cma *fbdev_cma;
 	struct drm_fb_helper *helper;
@@ -534,11 +544,17 @@ struct drm_fbdev_cma *drm_fbdev_cma_init_with_funcs(struct drm_device *dev,
 		dev_err(dev->dev, "Failed to allocate drm fbdev.\n");
 		return ERR_PTR(-ENOMEM);
 	}
-	fbdev_cma->fb_funcs = funcs;
+
+	if (!framebuffer_funcs)
+		framebuffer_funcs = &drm_fb_cma_funcs;
+	if (!fb_helper_funcs)
+		fb_helper_funcs = &drm_fb_cma_helper_funcs;
+
+	fbdev_cma->fb_funcs = framebuffer_funcs;
 
 	helper = &fbdev_cma->fb_helper;
 
-	drm_fb_helper_prepare(dev, helper, &drm_fb_cma_helper_funcs);
+	drm_fb_helper_prepare(dev, helper, fb_helper_funcs);
 
 	ret = drm_fb_helper_init(dev, helper, max_conn_count);
 	if (ret < 0) {
@@ -568,6 +584,25 @@ struct drm_fbdev_cma *drm_fbdev_cma_init_with_funcs(struct drm_device *dev,
 
 	return ERR_PTR(ret);
 }
+EXPORT_SYMBOL_GPL(drm_fbdev_cma_init_with_funcs2);
+
+/**
+ * drm_fbdev_cma_init_with_funcs() - Allocate and initializes a drm_fbdev_cma struct
+ * @dev: DRM device
+ * @preferred_bpp: Preferred bits per pixel for the device
+ * @max_conn_count: Maximum number of connectors
+ * @framebuffer_funcs: framebuffer functions, in particular a custom dirty() callback
+ *
+ * Returns a newly allocated drm_fbdev_cma struct or a ERR_PTR.
+ */
+struct drm_fbdev_cma *drm_fbdev_cma_init_with_funcs(struct drm_device *dev,
+	unsigned int preferred_bpp, unsigned int max_conn_count,
+	const struct drm_framebuffer_funcs *framebuffer_funcs)
+{
+	return drm_fbdev_cma_init_with_funcs2(dev, preferred_bpp,
+					      max_conn_count,
+					      framebuffer_funcs, NULL);
+}
 EXPORT_SYMBOL_GPL(drm_fbdev_cma_init_with_funcs);
 
 /**
@@ -581,9 +616,9 @@ EXPORT_SYMBOL_GPL(drm_fbdev_cma_init_with_funcs);
 struct drm_fbdev_cma *drm_fbdev_cma_init(struct drm_device *dev,
 	unsigned int preferred_bpp, unsigned int max_conn_count)
 {
-	return drm_fbdev_cma_init_with_funcs(dev, preferred_bpp,
-					     max_conn_count,
-					     &drm_fb_cma_funcs);
+	return drm_fbdev_cma_init_with_funcs2(dev, preferred_bpp,
+					      max_conn_count,
+					      NULL, NULL);
 }
 EXPORT_SYMBOL_GPL(drm_fbdev_cma_init);
 
diff --git a/include/drm/drm_fb_cma_helper.h b/include/drm/drm_fb_cma_helper.h
index 199a63f..280ec2b 100644
--- a/include/drm/drm_fb_cma_helper.h
+++ b/include/drm/drm_fb_cma_helper.h
@@ -15,13 +15,19 @@ struct drm_mode_fb_cmd2;
 struct drm_plane;
 struct drm_plane_state;
 
+struct drm_fbdev_cma *drm_fbdev_cma_init_with_funcs2(struct drm_device *dev,
+	unsigned int preferred_bpp, unsigned int max_conn_count,
+	const struct drm_framebuffer_funcs *framebuffer_funcs,
+	const struct drm_fb_helper_funcs *fb_helper_funcs);
 struct drm_fbdev_cma *drm_fbdev_cma_init_with_funcs(struct drm_device *dev,
 	unsigned int preferred_bpp, unsigned int max_conn_count,
-	const struct drm_framebuffer_funcs *funcs);
+	const struct drm_framebuffer_funcs *framebuffer_funcs);
 struct drm_fbdev_cma *drm_fbdev_cma_init(struct drm_device *dev,
 	unsigned int preferred_bpp, unsigned int max_conn_count);
 void drm_fbdev_cma_fini(struct drm_fbdev_cma *fbdev_cma);
 
+int drm_fbdev_cma_create(struct drm_fb_helper *helper,
+	struct drm_fb_helper_surface_size *sizes);
 void drm_fbdev_cma_restore_mode(struct drm_fbdev_cma *fbdev_cma);
 void drm_fbdev_cma_hotplug_event(struct drm_fbdev_cma *fbdev_cma);
 void drm_fbdev_cma_set_suspend(struct drm_fbdev_cma *fbdev_cma, int state);
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* [PATCH v3 3/3] drm: atmel-hlcdc: add clut support for legacy fbdev
  2017-06-19  7:44 [PATCH v3 0/3] drm: atmel-hlcdc: clut support Peter Rosin
  2017-06-19  7:44 ` [PATCH v3 1/3] drm: atmel-hlcdc: add support for 8-bit color lookup table mode Peter Rosin
  2017-06-19  7:44 ` [PATCH v3 2/3] drm/fb-cma-helper: expose more of fb cma guts Peter Rosin
@ 2017-06-19  7:44 ` Peter Rosin
  2017-06-19  7:48 ` [PATCH v3 0/3] drm: atmel-hlcdc: clut support Boris Brezillon
  2017-06-19 20:09 ` Boris Brezillon
  4 siblings, 0 replies; 7+ messages in thread
From: Peter Rosin @ 2017-06-19  7:44 UTC (permalink / raw)
  To: linux-kernel
  Cc: Peter Rosin, Boris Brezillon, David Airlie, Daniel Vetter,
	Jani Nikula, Sean Paul, dri-devel

The clut is not synchronized with the drm gamma_lut state.

Signed-off-by: Peter Rosin <peda@axentia.se>
---
 drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_crtc.c | 53 ++++++++++++++++++++++++++
 drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_dc.c   | 12 +++++-
 drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_dc.h   |  4 ++
 3 files changed, 67 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_crtc.c b/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_crtc.c
index 694adcc..4bee26e 100644
--- a/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_crtc.c
+++ b/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_crtc.c
@@ -140,6 +140,58 @@ static void atmel_hlcdc_crtc_mode_set_nofb(struct drm_crtc *c)
 			   cfg);
 }
 
+static void
+atmel_hlcdc_crtc_load_lut(struct drm_crtc *c)
+{
+	struct atmel_hlcdc_crtc *crtc = drm_crtc_to_atmel_hlcdc_crtc(c);
+	struct atmel_hlcdc_dc *dc = crtc->dc;
+	uint16_t *red = c->gamma_store;
+	uint16_t *green = red + c->gamma_size;
+	uint16_t *blue = green + c->gamma_size;
+	int layer;
+	int idx;
+
+	for (layer = 0; layer < ATMEL_HLCDC_MAX_LAYERS; layer++) {
+		if (!dc->layers[layer])
+			continue;
+
+		for (idx = 0; idx < ATMEL_HLCDC_CLUT_SIZE; idx++) {
+			u32 val = ((red[idx] << 8) & 0xff0000) |
+				(green[idx] & 0xff00) |
+				(blue[idx] >> 8);
+
+			atmel_hlcdc_layer_write_clut(dc->layers[layer],
+						     idx, val);
+		}
+	}
+}
+
+void atmel_hlcdc_gamma_set(struct drm_crtc *c,
+			   u16 r, u16 g, u16 b, int idx)
+{
+	if (idx < 0 || idx >= c->gamma_size)
+		return;
+
+	c->gamma_store[idx] = r;
+	idx += c->gamma_size;
+	c->gamma_store[idx] = g;
+	idx += c->gamma_size;
+	c->gamma_store[idx] = b;
+}
+
+void atmel_hlcdc_gamma_get(struct drm_crtc *c,
+			   u16 *r, u16 *g, u16 *b, int idx)
+{
+	if (idx < 0 || idx >= c->gamma_size)
+		return;
+
+	*r = c->gamma_store[idx];
+	idx += c->gamma_size;
+	*g = c->gamma_store[idx];
+	idx += c->gamma_size;
+	*b = c->gamma_store[idx];
+}
+
 static enum drm_mode_status
 atmel_hlcdc_crtc_mode_valid(struct drm_crtc *c,
 			    const struct drm_display_mode *mode)
@@ -319,6 +371,7 @@ static const struct drm_crtc_helper_funcs lcdc_crtc_helper_funcs = {
 	.mode_set = drm_helper_crtc_mode_set,
 	.mode_set_nofb = atmel_hlcdc_crtc_mode_set_nofb,
 	.mode_set_base = drm_helper_crtc_mode_set_base,
+	.load_lut = atmel_hlcdc_crtc_load_lut,
 	.disable = atmel_hlcdc_crtc_disable,
 	.enable = atmel_hlcdc_crtc_enable,
 	.atomic_check = atmel_hlcdc_crtc_atomic_check,
diff --git a/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_dc.c b/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_dc.c
index 4f6ef07..9a09c73 100644
--- a/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_dc.c
+++ b/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_dc.c
@@ -601,6 +601,12 @@ static int atmel_hlcdc_dc_modeset_init(struct drm_device *dev)
 	return 0;
 }
 
+static const struct drm_fb_helper_funcs atmel_hlcdc_fb_cma_helper_funcs = {
+	.gamma_set	= atmel_hlcdc_gamma_set,
+	.gamma_get	= atmel_hlcdc_gamma_get,
+	.fb_probe	= drm_fbdev_cma_create,
+};
+
 static int atmel_hlcdc_dc_load(struct drm_device *dev)
 {
 	struct platform_device *pdev = to_platform_device(dev->dev);
@@ -664,8 +670,10 @@ static int atmel_hlcdc_dc_load(struct drm_device *dev)
 
 	platform_set_drvdata(pdev, dev);
 
-	dc->fbdev = drm_fbdev_cma_init(dev, 24,
-			dev->mode_config.num_connector);
+	dc->fbdev = drm_fbdev_cma_init_with_funcs2(dev, 24,
+			dev->mode_config.num_connector,
+			NULL,
+			&atmel_hlcdc_fb_cma_helper_funcs);
 	if (IS_ERR(dc->fbdev))
 		dc->fbdev = NULL;
 
diff --git a/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_dc.h b/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_dc.h
index 4237b04..fb57c6e 100644
--- a/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_dc.h
+++ b/drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_dc.h
@@ -32,6 +32,7 @@
 #include <drm/drm_atomic_helper.h>
 #include <drm/drm_crtc.h>
 #include <drm/drm_crtc_helper.h>
+#include <drm/drm_fb_helper.h>
 #include <drm/drm_fb_cma_helper.h>
 #include <drm/drm_gem_cma_helper.h>
 #include <drm/drm_panel.h>
@@ -448,6 +449,9 @@ void atmel_hlcdc_plane_irq(struct atmel_hlcdc_plane *plane);
 int atmel_hlcdc_plane_prepare_disc_area(struct drm_crtc_state *c_state);
 int atmel_hlcdc_plane_prepare_ahb_routing(struct drm_crtc_state *c_state);
 
+void atmel_hlcdc_gamma_set(struct drm_crtc *c, u16 r, u16 g, u16 b, int idx);
+void atmel_hlcdc_gamma_get(struct drm_crtc *c, u16 *r, u16 *g, u16 *b, int idx);
+
 void atmel_hlcdc_crtc_irq(struct drm_crtc *c);
 
 int atmel_hlcdc_crtc_create(struct drm_device *dev);
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: [PATCH v3 0/3] drm: atmel-hlcdc: clut support
  2017-06-19  7:44 [PATCH v3 0/3] drm: atmel-hlcdc: clut support Peter Rosin
                   ` (2 preceding siblings ...)
  2017-06-19  7:44 ` [PATCH v3 3/3] drm: atmel-hlcdc: add clut support for legacy fbdev Peter Rosin
@ 2017-06-19  7:48 ` Boris Brezillon
  2017-06-19 20:09 ` Boris Brezillon
  4 siblings, 0 replies; 7+ messages in thread
From: Boris Brezillon @ 2017-06-19  7:48 UTC (permalink / raw)
  To: Peter Rosin
  Cc: linux-kernel, David Airlie, Daniel Vetter, Jani Nikula, Sean Paul,
	dri-devel, Alexandre Belloni, Nicolas Ferre

+Alexandre and Nicolas

Hi Peter,

Can you please Cc at91 maintainers next time?

On Mon, 19 Jun 2017 09:44:23 +0200
Peter Rosin <peda@axentia.se> wrote:

> Hi!
> 
> This series adds support for an 8-bit clut mode in the atmel-hlcdc
> driver.
> 
> I have now tested patch 1 with the below program (modeset.c
> adapted from https://github.com/dvdhrm/docs/tree/master/drm-howto
> to use an 8-bit mode).
> 
> Since v2 I have also cleared up why the first 16 entries of the clut
> was not working right. It was of course my own damn fault, and the
> fix was in atmel_hlcdc_layer_write_clut function which called the
> ...write_reg function which in turn added an extra offset of 16
> registers...
> 
> Changes since v2:
> 
> - Fix mapping to the clut registers.
> 
> Changes since v1:
> 
> - Move the clut update from atmel_hlcdc_crtc_mode_valid to
>   atmel_hlcdc_plane_atomic_update.
> - Add default .gamma_set helper (drm_atomic_helper_legacy_gamma_set).
> - Don't keep a spare copy of the clut, reuse gamma_store instead.
> - Don't try to synchronize the legacy fb clut with the drm clut.
> 
> As I said in v2, I have not added any .clut_offset to the overlay2
> layer of sama5d4, since the chip does not appear to have that layer.
> I didn't do that to make it easier to work with the patch previously
> sent to remove that layer, but I suspect bad things may happen to
> sama5d4 users if they do not have that layer removed.
> 
> Cheers,
> peda
> 
> modeset-pal.c (didn't update any comments, sorry)
> ----------------8<---------------
> /*
>  * modeset - DRM Modesetting Example
>  *
>  * Written 2012 by David Herrmann <dh.herrmann@googlemail.com>
>  * Dedicated to the Public Domain.
>  */
> 
> /*
>  * DRM Modesetting Howto
>  * This document describes the DRM modesetting API. Before we can use the DRM
>  * API, we have to include xf86drm.h and xf86drmMode.h. Both are provided by
>  * libdrm which every major distribution ships by default. It has no other
>  * dependencies and is pretty small.
>  *
>  * Please ignore all forward-declarations of functions which are used later. I
>  * reordered the functions so you can read this document from top to bottom. If
>  * you reimplement it, you would probably reorder the functions to avoid all the
>  * nasty forward declarations.
>  *
>  * For easier reading, we ignore all memory-allocation errors of malloc() and
>  * friends here. However, we try to correctly handle all other kinds of errors
>  * that may occur.
>  *
>  * All functions and global variables are prefixed with "modeset_*" in this
>  * file. So it should be clear whether a function is a local helper or if it is
>  * provided by some external library.
>  */
> 
> #define _GNU_SOURCE
> #include <errno.h>
> #include <fcntl.h>
> #include <stdbool.h>
> #include <stdint.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> #include <sys/mman.h>
> #include <time.h>
> #include <unistd.h>
> #include <xf86drm.h>
> #include <xf86drmMode.h>
> 
> struct modeset_dev;
> static int modeset_find_crtc(int fd, drmModeRes *res, drmModeConnector *conn,
> 			     struct modeset_dev *dev);
> static int modeset_create_fb(int fd, struct modeset_dev *dev);
> static int modeset_setup_dev(int fd, drmModeRes *res, drmModeConnector *conn,
> 			     struct modeset_dev *dev);
> static int modeset_open(int *out, const char *node);
> static int modeset_prepare(int fd);
> static void modeset_draw(int fd);
> static void modeset_cleanup(int fd);
> 
> /*
>  * When the linux kernel detects a graphics-card on your machine, it loads the
>  * correct device driver (located in kernel-tree at ./drivers/gpu/drm/<xy>) and
>  * provides two character-devices to control it. Udev (or whatever hotplugging
>  * application you use) will create them as:
>  *     /dev/dri/card0
>  *     /dev/dri/controlID64
>  * We only need the first one. You can hard-code this path into your application
>  * like we do here, but it is recommended to use libudev with real hotplugging
>  * and multi-seat support. However, this is beyond the scope of this document.
>  * Also note that if you have multiple graphics-cards, there may also be
>  * /dev/dri/card1, /dev/dri/card2, ...
>  *
>  * We simply use /dev/dri/card0 here but the user can specify another path on
>  * the command line.
>  *
>  * modeset_open(out, node): This small helper function opens the DRM device
>  * which is given as @node. The new fd is stored in @out on success. On failure,
>  * a negative error code is returned.
>  * After opening the file, we also check for the DRM_CAP_DUMB_BUFFER capability.
>  * If the driver supports this capability, we can create simple memory-mapped
>  * buffers without any driver-dependent code. As we want to avoid any radeon,
>  * nvidia, intel, etc. specific code, we depend on DUMB_BUFFERs here.
>  */
> 
> static int modeset_open(int *out, const char *node)
> {
> 	int fd, ret;
> 	uint64_t has_dumb;
> 
> 	fd = open(node, O_RDWR | O_CLOEXEC);
> 	if (fd < 0) {
> 		ret = -errno;
> 		fprintf(stderr, "cannot open '%s': %m\n", node);
> 		return ret;
> 	}
> 
> 	if (drmGetCap(fd, DRM_CAP_DUMB_BUFFER, &has_dumb) < 0 ||
> 	    !has_dumb) {
> 		fprintf(stderr, "drm device '%s' does not support dumb buffers\n",
> 			node);
> 		close(fd);
> 		return -EOPNOTSUPP;
> 	}
> 
> 	*out = fd;
> 	return 0;
> }
> 
> /*
>  * As a next step we need to find our available display devices. libdrm provides
>  * a drmModeRes structure that contains all the needed information. We can
>  * retrieve it via drmModeGetResources(fd) and free it via
>  * drmModeFreeResources(res) again.
>  *
>  * A physical connector on your graphics card is called a "connector". You can
>  * plug a monitor into it and control what is displayed. We are definitely
>  * interested in what connectors are currently used, so we simply iterate
>  * through the list of connectors and try to display a test-picture on each
>  * available monitor.
>  * However, this isn't as easy as it sounds. First, we need to check whether the
>  * connector is actually used (a monitor is plugged in and turned on). Then we
>  * need to find a CRTC that can control this connector. CRTCs are described
>  * later on. After that we create a framebuffer object. If we have all this, we
>  * can mmap() the framebuffer and draw a test-picture into it. Then we can tell
>  * the DRM device to show the framebuffer on the given CRTC with the selected
>  * connector.
>  *
>  * As we want to draw moving pictures on the framebuffer, we actually have to
>  * remember all these settings. Therefore, we create one "struct modeset_dev"
>  * object for each connector+crtc+framebuffer pair that we successfully
>  * initialized and push it into the global device-list.
>  *
>  * Each field of this structure is described when it is first used. But as a
>  * summary:
>  * "struct modeset_dev" contains: {
>  *  - @next: points to the next device in the single-linked list
>  *
>  *  - @width: width of our buffer object
>  *  - @height: height of our buffer object
>  *  - @stride: stride value of our buffer object
>  *  - @size: size of the memory mapped buffer
>  *  - @handle: a DRM handle to the buffer object that we can draw into
>  *  - @map: pointer to the memory mapped buffer
>  *
>  *  - @mode: the display mode that we want to use
>  *  - @fb: a framebuffer handle with our buffer object as scanout buffer
>  *  - @conn: the connector ID that we want to use with this buffer
>  *  - @crtc: the crtc ID that we want to use with this connector
>  *  - @saved_crtc: the configuration of the crtc before we changed it. We use it
>  *                 so we can restore the same mode when we exit.
>  * }
>  */
> 
> struct modeset_dev {
> 	struct modeset_dev *next;
> 
> 	uint32_t width;
> 	uint32_t height;
> 	uint32_t stride;
> 	uint32_t size;
> 	uint32_t handle;
> 	uint8_t *map;
> 
> 	drmModeModeInfo mode;
> 	uint32_t fb;
> 	uint32_t conn;
> 	uint32_t crtc;
> 	drmModeCrtc *saved_crtc;
> };
> 
> static struct modeset_dev *modeset_list = NULL;
> 
> /*
>  * So as next step we need to actually prepare all connectors that we find. We
>  * do this in this little helper function:
>  *
>  * modeset_prepare(fd): This helper function takes the DRM fd as argument and
>  * then simply retrieves the resource-info from the device. It then iterates
>  * through all connectors and calls other helper functions to initialize this
>  * connector (described later on).
>  * If the initialization was successful, we simply add this object as new device
>  * into the global modeset device list.
>  *
>  * The resource-structure contains a list of all connector-IDs. We use the
>  * helper function drmModeGetConnector() to retrieve more information on each
>  * connector. After we are done with it, we free it again with
>  * drmModeFreeConnector().
>  * Our helper modeset_setup_dev() returns -ENOENT if the connector is currently
>  * unused and no monitor is plugged in. So we can ignore this connector.
>  */
> 
> static int modeset_prepare(int fd)
> {
> 	drmModeRes *res;
> 	drmModeConnector *conn;
> 	unsigned int i;
> 	struct modeset_dev *dev;
> 	int ret;
> 
> 	/* retrieve resources */
> 	res = drmModeGetResources(fd);
> 	if (!res) {
> 		fprintf(stderr, "cannot retrieve DRM resources (%d): %m\n",
> 			errno);
> 		return -errno;
> 	}
> 
> 	/* iterate all connectors */
> 	for (i = 0; i < res->count_connectors; ++i) {
> 		/* get information for each connector */
> 		conn = drmModeGetConnector(fd, res->connectors[i]);
> 		if (!conn) {
> 			fprintf(stderr, "cannot retrieve DRM connector %u:%u (%d): %m\n",
> 				i, res->connectors[i], errno);
> 			continue;
> 		}
> 
> 		/* create a device structure */
> 		dev = malloc(sizeof(*dev));
> 		memset(dev, 0, sizeof(*dev));
> 		dev->conn = conn->connector_id;
> 
> 		/* call helper function to prepare this connector */
> 		ret = modeset_setup_dev(fd, res, conn, dev);
> 		if (ret) {
> 			if (ret != -ENOENT) {
> 				errno = -ret;
> 				fprintf(stderr, "cannot setup device for connector %u:%u (%d): %m\n",
> 					i, res->connectors[i], errno);
> 			}
> 			free(dev);
> 			drmModeFreeConnector(conn);
> 			continue;
> 		}
> 
> 		/* free connector data and link device into global list */
> 		drmModeFreeConnector(conn);
> 		dev->next = modeset_list;
> 		modeset_list = dev;
> 	}
> 
> 	/* free resources again */
> 	drmModeFreeResources(res);
> 	return 0;
> }
> 
> /*
>  * Now we dig deeper into setting up a single connector. As described earlier,
>  * we need to check several things first:
>  *   * If the connector is currently unused, that is, no monitor is plugged in,
>  *     then we can ignore it.
>  *   * We have to find a suitable resolution and refresh-rate. All this is
>  *     available in drmModeModeInfo structures saved for each crtc. We simply
>  *     use the first mode that is available. This is always the mode with the
>  *     highest resolution.
>  *     A more sophisticated mode-selection should be done in real applications,
>  *     though.
>  *   * Then we need to find an CRTC that can drive this connector. A CRTC is an
>  *     internal resource of each graphics-card. The number of CRTCs controls how
>  *     many connectors can be controlled indepedently. That is, a graphics-cards
>  *     may have more connectors than CRTCs, which means, not all monitors can be
>  *     controlled independently.
>  *     There is actually the possibility to control multiple connectors via a
>  *     single CRTC if the monitors should display the same content. However, we
>  *     do not make use of this here.
>  *     So think of connectors as pipelines to the connected monitors and the
>  *     CRTCs are the controllers that manage which data goes to which pipeline.
>  *     If there are more pipelines than CRTCs, then we cannot control all of
>  *     them at the same time.
>  *   * We need to create a framebuffer for this connector. A framebuffer is a
>  *     memory buffer that we can write XRGB32 data into. So we use this to
>  *     render our graphics and then the CRTC can scan-out this data from the
>  *     framebuffer onto the monitor.
>  */
> 
> static int modeset_setup_dev(int fd, drmModeRes *res, drmModeConnector *conn,
> 			     struct modeset_dev *dev)
> {
> 	int ret;
> 
> 	/* check if a monitor is connected */
> 	if (conn->connection != DRM_MODE_CONNECTED) {
> 		fprintf(stderr, "ignoring unused connector %u\n",
> 			conn->connector_id);
> 		return -ENOENT;
> 	}
> 
> 	/* check if there is at least one valid mode */
> 	if (conn->count_modes == 0) {
> 		fprintf(stderr, "no valid mode for connector %u\n",
> 			conn->connector_id);
> 		return -EFAULT;
> 	}
> 
> 	/* copy the mode information into our device structure */
> 	memcpy(&dev->mode, &conn->modes[0], sizeof(dev->mode));
> 	dev->width = conn->modes[0].hdisplay;
> 	dev->height = conn->modes[0].vdisplay;
> 	fprintf(stderr, "mode for connector %u is %ux%u\n",
> 		conn->connector_id, dev->width, dev->height);
> 
> 	/* find a crtc for this connector */
> 	ret = modeset_find_crtc(fd, res, conn, dev);
> 	if (ret) {
> 		fprintf(stderr, "no valid crtc for connector %u\n",
> 			conn->connector_id);
> 		return ret;
> 	}
> 
> 	/* create a framebuffer for this CRTC */
> 	ret = modeset_create_fb(fd, dev);
> 	if (ret) {
> 		fprintf(stderr, "cannot create framebuffer for connector %u\n",
> 			conn->connector_id);
> 		return ret;
> 	}
> 
> 	return 0;
> }
> 
> /*
>  * modeset_find_crtc(fd, res, conn, dev): This small helper tries to find a
>  * suitable CRTC for the given connector. We have actually have to introduce one
>  * more DRM object to make this more clear: Encoders.
>  * Encoders help the CRTC to convert data from a framebuffer into the right
>  * format that can be used for the chosen connector. We do not have to
>  * understand any more of these conversions to make use of it. However, you must
>  * know that each connector has a limited list of encoders that it can use. And
>  * each encoder can only work with a limited list of CRTCs. So what we do is
>  * trying each encoder that is available and looking for a CRTC that this
>  * encoder can work with. If we find the first working combination, we are happy
>  * and write it into the @dev structure.
>  * But before iterating all available encoders, we first try the currently
>  * active encoder+crtc on a connector to avoid a full modeset.
>  *
>  * However, before we can use a CRTC we must make sure that no other device,
>  * that we setup previously, is already using this CRTC. Remember, we can only
>  * drive one connector per CRTC! So we simply iterate through the "modeset_list"
>  * of previously setup devices and check that this CRTC wasn't used before.
>  * Otherwise, we continue with the next CRTC/Encoder combination.
>  */
> 
> static int modeset_find_crtc(int fd, drmModeRes *res, drmModeConnector *conn,
> 			     struct modeset_dev *dev)
> {
> 	drmModeEncoder *enc;
> 	unsigned int i, j;
> 	int32_t crtc;
> 	struct modeset_dev *iter;
> 
> 	/* first try the currently conected encoder+crtc */
> 	if (conn->encoder_id)
> 		enc = drmModeGetEncoder(fd, conn->encoder_id);
> 	else
> 		enc = NULL;
> 
> 	if (enc) {
> 		if (enc->crtc_id) {
> 			crtc = enc->crtc_id;
> 			for (iter = modeset_list; iter; iter = iter->next) {
> 				if (iter->crtc == crtc) {
> 					crtc = -1;
> 					break;
> 				}
> 			}
> 
> 			if (crtc >= 0) {
> 				drmModeFreeEncoder(enc);
> 				dev->crtc = crtc;
> 				return 0;
> 			}
> 		}
> 
> 		drmModeFreeEncoder(enc);
> 	}
> 
> 	/* If the connector is not currently bound to an encoder or if the
> 	 * encoder+crtc is already used by another connector (actually unlikely
> 	 * but lets be safe), iterate all other available encoders to find a
> 	 * matching CRTC. */
> 	for (i = 0; i < conn->count_encoders; ++i) {
> 		enc = drmModeGetEncoder(fd, conn->encoders[i]);
> 		if (!enc) {
> 			fprintf(stderr, "cannot retrieve encoder %u:%u (%d): %m\n",
> 				i, conn->encoders[i], errno);
> 			continue;
> 		}
> 
> 		/* iterate all global CRTCs */
> 		for (j = 0; j < res->count_crtcs; ++j) {
> 			/* check whether this CRTC works with the encoder */
> 			if (!(enc->possible_crtcs & (1 << j)))
> 				continue;
> 
> 			/* check that no other device already uses this CRTC */
> 			crtc = res->crtcs[j];
> 			for (iter = modeset_list; iter; iter = iter->next) {
> 				if (iter->crtc == crtc) {
> 					crtc = -1;
> 					break;
> 				}
> 			}
> 
> 			/* we have found a CRTC, so save it and return */
> 			if (crtc >= 0) {
> 				drmModeFreeEncoder(enc);
> 				dev->crtc = crtc;
> 				return 0;
> 			}
> 		}
> 
> 		drmModeFreeEncoder(enc);
> 	}
> 
> 	fprintf(stderr, "cannot find suitable CRTC for connector %u\n",
> 		conn->connector_id);
> 	return -ENOENT;
> }
> 
> /*
>  * modeset_create_fb(fd, dev): After we have found a crtc+connector+mode
>  * combination, we need to actually create a suitable framebuffer that we can
>  * use with it. There are actually two ways to do that:
>  *   * We can create a so called "dumb buffer". This is a buffer that we can
>  *     memory-map via mmap() and every driver supports this. We can use it for
>  *     unaccelerated software rendering on the CPU.
>  *   * We can use libgbm to create buffers available for hardware-acceleration.
>  *     libgbm is an abstraction layer that creates these buffers for each
>  *     available DRM driver. As there is no generic API for this, each driver
>  *     provides its own way to create these buffers.
>  *     We can then use such buffers to create OpenGL contexts with the mesa3D
>  *     library.
>  * We use the first solution here as it is much simpler and doesn't require any
>  * external libraries. However, if you want to use hardware-acceleration via
>  * OpenGL, it is actually pretty easy to create such buffers with libgbm and
>  * libEGL. But this is beyond the scope of this document.
>  *
>  * So what we do is requesting a new dumb-buffer from the driver. We specify the
>  * same size as the current mode that we selected for the connector.
>  * Then we request the driver to prepare this buffer for memory mapping. After
>  * that we perform the actual mmap() call. So we can now access the framebuffer
>  * memory directly via the dev->map memory map.
>  */
> 
> static int modeset_create_fb(int fd, struct modeset_dev *dev)
> {
> 	struct drm_mode_create_dumb creq;
> 	struct drm_mode_destroy_dumb dreq;
> 	struct drm_mode_map_dumb mreq;
> 	int ret;
> 
> 	/* create dumb buffer */
> 	memset(&creq, 0, sizeof(creq));
> 	creq.width = dev->width;
> 	creq.height = dev->height;
> 	creq.bpp = 8;
> 	ret = drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &creq);
> 	if (ret < 0) {
> 		fprintf(stderr, "cannot create dumb buffer (%d): %m\n",
> 			errno);
> 		return -errno;
> 	}
> 	dev->stride = creq.pitch;
> 	dev->size = creq.size;
> 	dev->handle = creq.handle;
> 
> 	/* create framebuffer object for the dumb-buffer */
> 	ret = drmModeAddFB(fd, dev->width, dev->height, 8, 8, dev->stride,
> 			   dev->handle, &dev->fb);
> 	if (ret) {
> 		fprintf(stderr, "cannot create framebuffer (%d): %m\n",
> 			errno);
> 		ret = -errno;
> 		goto err_destroy;
> 	}
> 
> 	/* prepare buffer for memory mapping */
> 	memset(&mreq, 0, sizeof(mreq));
> 	mreq.handle = dev->handle;
> 	ret = drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &mreq);
> 	if (ret) {
> 		fprintf(stderr, "cannot map dumb buffer (%d): %m\n",
> 			errno);
> 		ret = -errno;
> 		goto err_fb;
> 	}
> 
> 	/* perform actual memory mapping */
> 	dev->map = mmap(0, dev->size, PROT_READ | PROT_WRITE, MAP_SHARED,
> 		        fd, mreq.offset);
> 	if (dev->map == MAP_FAILED) {
> 		fprintf(stderr, "cannot mmap dumb buffer (%d): %m\n",
> 			errno);
> 		ret = -errno;
> 		goto err_fb;
> 	}
> 
> 	/* clear the framebuffer to 0 */
> 	memset(dev->map, 0, dev->size);
> 
> 	return 0;
> 
> err_fb:
> 	drmModeRmFB(fd, dev->fb);
> err_destroy:
> 	memset(&dreq, 0, sizeof(dreq));
> 	dreq.handle = dev->handle;
> 	drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);
> 	return ret;
> }
> 
> /*
>  * Finally! We have a connector with a suitable CRTC. We know which mode we want
>  * to use and we have a framebuffer of the correct size that we can write to.
>  * There is nothing special left to do. We only have to program the CRTC to
>  * connect each new framebuffer to each selected connector for each combination
>  * that we saved in the global modeset_list.
>  * This is done with a call to drmModeSetCrtc().
>  *
>  * So we are ready for our main() function. First we check whether the user
>  * specified a DRM device on the command line, otherwise we use the default
>  * /dev/dri/card0. Then we open the device via modeset_open(). modeset_prepare()
>  * prepares all connectors and we can loop over "modeset_list" and call
>  * drmModeSetCrtc() on every CRTC/connector combination.
>  *
>  * But printing empty black pages is boring so we have another helper function
>  * modeset_draw() that draws some colors into the framebuffer for 5 seconds and
>  * then returns. And then we have all the cleanup functions which correctly free
>  * all devices again after we used them. All these functions are described below
>  * the main() function.
>  *
>  * As a side note: drmModeSetCrtc() actually takes a list of connectors that we
>  * want to control with this CRTC. We pass only one connector, though. As
>  * explained earlier, if we used multiple connectors, then all connectors would
>  * have the same controlling framebuffer so the output would be cloned. This is
>  * most often not what you want so we avoid explaining this feature here.
>  * Furthermore, all connectors will have to run with the same mode, which is
>  * also often not guaranteed. So instead, we only use one connector per CRTC.
>  *
>  * Before calling drmModeSetCrtc() we also save the current CRTC configuration.
>  * This is used in modeset_cleanup() to restore the CRTC to the same mode as was
>  * before we changed it.
>  * If we don't do this, the screen will stay blank after we exit until another
>  * application performs modesetting itself.
>  */
> 
> int main(int argc, char **argv)
> {
> 	int ret, fd;
> 	const char *card;
> 	struct modeset_dev *iter;
> 
> 	/* check which DRM device to open */
> 	if (argc > 1)
> 		card = argv[1];
> 	else
> 		card = "/dev/dri/card0";
> 
> 	fprintf(stderr, "using card '%s'\n", card);
> 
> 	/* open the DRM device */
> 	ret = modeset_open(&fd, card);
> 	if (ret)
> 		goto out_return;
> 
> 	/* prepare all connectors and CRTCs */
> 	ret = modeset_prepare(fd);
> 	if (ret)
> 		goto out_close;
> 
> 	/* perform actual modesetting on each found connector+CRTC */
> 	for (iter = modeset_list; iter; iter = iter->next) {
> 		iter->saved_crtc = drmModeGetCrtc(fd, iter->crtc);
> 		ret = drmModeSetCrtc(fd, iter->crtc, iter->fb, 0, 0,
> 				     &iter->conn, 1, &iter->mode);
> 		if (ret)
> 			fprintf(stderr, "cannot set CRTC for connector %u (%d): %m\n",
> 				iter->conn, errno);
> 	}
> 
> 	/* draw some colors for 5seconds */
> 	modeset_draw(fd);
> 
> 	/* cleanup everything */
> 	modeset_cleanup(fd);
> 
> 	ret = 0;
> 
> out_close:
> 	close(fd);
> out_return:
> 	if (ret) {
> 		errno = -ret;
> 		fprintf(stderr, "modeset failed with error %d: %m\n", errno);
> 	} else {
> 		fprintf(stderr, "exiting\n");
> 	}
> 	return ret;
> }
> 
> /*
>  * A short helper function to compute a changing color value. No need to
>  * understand it.
>  */
> 
> static uint8_t next_color(bool *up, uint8_t cur, unsigned int mod)
> {
> 	uint8_t next;
> 
> 	next = cur + (*up ? 1 : -1) * (rand() % mod);
> 	if ((*up && next < cur) || (!*up && next > cur)) {
> 		*up = !*up;
> 		next = cur;
> 	}
> 
> 	return next;
> }
> 
> static void crtc_lut(int fd, struct modeset_dev *dev, int p)
> {
> 	struct drm_mode_crtc_lut clut;
> 	uint16_t r[256];
> 	uint16_t g[256];
> 	uint16_t b[256];
> 	int ret;
> 	int i;
> 
> 	/* prepare buffer for memory mapping */
> 	memset(&clut, 0, sizeof(clut));
> 	clut.crtc_id = dev->crtc;
> 	clut.gamma_size = 256;
> 	clut.red = (uint64_t)r;
> 	clut.green = (uint64_t)g;
> 	clut.blue = (uint64_t)b;
> 
> 	for (i = 0; i < 256; ++i) {
> 		r[i] = ((p + 2 * i) & 255) * 257;
> 		g[i] = ((p + 3 * i) & 255) * 257;
> 		b[i] = ((p + 5 * i) & 255) * 257;
> 	}
> 	ret = drmIoctl(fd, DRM_IOCTL_MODE_SETGAMMA, &clut);
> 	if (ret)
> 		fprintf(stderr, "cannot set gamma lut (%d): %m\n",
> 			errno);
> }
> 
> /*
>  * modeset_draw(): This draws a solid color into all configured framebuffers.
>  * Every 100ms the color changes to a slightly different color so we get some
>  * kind of smoothly changing color-gradient.
>  *
>  * The color calculation can be ignored as it is pretty boring. So the
>  * interesting stuff is iterating over "modeset_list" and then through all lines
>  * and width. We then set each pixel individually to the current color.
>  *
>  * We do this 50 times as we sleep 100ms after each redraw round. This makes
>  * 50*100ms = 5000ms = 5s so it takes about 5seconds to finish this loop.
>  *
>  * Please note that we draw directly into the framebuffer. This means that you
>  * will see flickering as the monitor might refresh while we redraw the screen.
>  * To avoid this you would need to use two framebuffers and a call to
>  * drmModeSetCrtc() to switch between both buffers.
>  * You can also use drmModePageFlip() to do a vsync'ed pageflip. But this is
>  * beyond the scope of this document.
>  */
> 
> static void modeset_draw(int fd)
> {
> 	uint8_t p = 0;
> 	unsigned int i, j, k;
> 	struct modeset_dev *iter;
> 
> 	for (iter = modeset_list; iter; iter = iter->next) {
> 		for (k = 0; k < iter->width; ++k) {
> 			for (j = 0; j < iter->height / 3; ++j) {
> 				iter->map[iter->stride * j + k] =
> 					k * 256 / iter->width;
> 			}
> 			for (; j < iter->height; ++j)
> 				iter->map[iter->stride * j + k] = 26;
> 		}
> 	}
> 
> 	for (i = 0; i < 50; ++i, ++p) {
> 		for (iter = modeset_list; iter; iter = iter->next)
> 			crtc_lut(fd, iter, p);
> 
> 		usleep(100000);
> 	}
> }
> 
> /*
>  * modeset_cleanup(fd): This cleans up all the devices we created during
>  * modeset_prepare(). It resets the CRTCs to their saved states and deallocates
>  * all memory.
>  * It should be pretty obvious how all of this works.
>  */
> 
> static void modeset_cleanup(int fd)
> {
> 	struct modeset_dev *iter;
> 	struct drm_mode_destroy_dumb dreq;
> 
> 	while (modeset_list) {
> 		/* remove from global list */
> 		iter = modeset_list;
> 		modeset_list = iter->next;
> 
> 		/* restore saved CRTC configuration */
> 		drmModeSetCrtc(fd,
> 			       iter->saved_crtc->crtc_id,
> 			       iter->saved_crtc->buffer_id,
> 			       iter->saved_crtc->x,
> 			       iter->saved_crtc->y,
> 			       &iter->conn,
> 			       1,
> 			       &iter->saved_crtc->mode);
> 		drmModeFreeCrtc(iter->saved_crtc);
> 
> 		/* unmap buffer */
> 		munmap(iter->map, iter->size);
> 
> 		/* delete framebuffer */
> 		drmModeRmFB(fd, iter->fb);
> 
> 		/* delete dumb buffer */
> 		memset(&dreq, 0, sizeof(dreq));
> 		dreq.handle = iter->handle;
> 		drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);
> 
> 		/* free allocated memory */
> 		free(iter);
> 	}
> }
> 
> /*
>  * I hope this was a short but easy overview of the DRM modesetting API. The DRM
>  * API offers much more capabilities including:
>  *  - double-buffering or tripple-buffering (or whatever you want)
>  *  - vsync'ed page-flips
>  *  - hardware-accelerated rendering (for example via OpenGL)
>  *  - output cloning
>  *  - graphics-clients plus authentication
>  *  - DRM planes/overlays/sprites
>  *  - ...
>  * If you are interested in these topics, I can currently only redirect you to
>  * existing implementations, including:
>  *  - plymouth (which uses dumb-buffers like this example; very easy to understand)
>  *  - kmscon (which uses libuterm to do this)
>  *  - wayland (very sophisticated DRM renderer; hard to understand fully as it
>  *             uses more complicated techniques like DRM planes)
>  *  - xserver (very hard to understand as it is split across many files/projects)
>  *
>  * But understanding how modesetting (as described in this document) works, is
>  * essential to understand all further DRM topics.
>  *
>  * Any feedback is welcome. Feel free to use this code freely for your own
>  * documentation or projects.
>  *
>  *  - Hosted on http://github.com/dvdhrm/docs
>  *  - Written by David Herrmann <dh.herrmann@googlemail.com>
>  */
> ----------------8<---------------
> 
> Peter Rosin (3):
>   drm: atmel-hlcdc: add support for 8-bit color lookup table mode
>   drm/fb-cma-helper: expose more of fb cma guts
>   drm: atmel-hlcdc: add clut support for legacy fbdev
> 
>  drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_crtc.c  | 58 +++++++++++++++++++++++++
>  drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_dc.c    | 25 ++++++++++-
>  drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_dc.h    | 20 +++++++++
>  drivers/gpu/drm/atmel-hlcdc/atmel_hlcdc_plane.c | 29 +++++++++++++
>  drivers/gpu/drm/drm_fb_cma_helper.c             | 55 ++++++++++++++++++-----
>  include/drm/drm_fb_cma_helper.h                 |  8 +++-
>  6 files changed, 182 insertions(+), 13 deletions(-)
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v3 0/3] drm: atmel-hlcdc: clut support
  2017-06-19  7:44 [PATCH v3 0/3] drm: atmel-hlcdc: clut support Peter Rosin
                   ` (3 preceding siblings ...)
  2017-06-19  7:48 ` [PATCH v3 0/3] drm: atmel-hlcdc: clut support Boris Brezillon
@ 2017-06-19 20:09 ` Boris Brezillon
  4 siblings, 0 replies; 7+ messages in thread
From: Boris Brezillon @ 2017-06-19 20:09 UTC (permalink / raw)
  To: Peter Rosin
  Cc: linux-kernel, David Airlie, Daniel Vetter, Jani Nikula, Sean Paul,
	dri-devel, Nicolas Ferre, Alexandre Belloni

Le Mon, 19 Jun 2017 09:44:23 +0200,
Peter Rosin <peda@axentia.se> a écrit :

> Hi!
> 
> This series adds support for an 8-bit clut mode in the atmel-hlcdc
> driver.
> 
> I have now tested patch 1 with the below program (modeset.c
> adapted from https://github.com/dvdhrm/docs/tree/master/drm-howto
> to use an 8-bit mode).

I'm glad you finally find a way to test it. Patch 1 looks good to me,
except I would have added the missing .set_property in a separate patch
(placed at the beginning of the series).

> 
> Since v2 I have also cleared up why the first 16 entries of the clut
> was not working right. It was of course my own damn fault, and the
> fix was in atmel_hlcdc_layer_write_clut function which called the
> ...write_reg function which in turn added an extra offset of 16
> registers...
> 
> Changes since v2:
> 
> - Fix mapping to the clut registers.
> 
> Changes since v1:
> 
> - Move the clut update from atmel_hlcdc_crtc_mode_valid to
>   atmel_hlcdc_plane_atomic_update.
> - Add default .gamma_set helper (drm_atomic_helper_legacy_gamma_set).
> - Don't keep a spare copy of the clut, reuse gamma_store instead.
> - Don't try to synchronize the legacy fb clut with the drm clut.
> 
> As I said in v2, I have not added any .clut_offset to the overlay2
> layer of sama5d4, since the chip does not appear to have that layer.
> I didn't do that to make it easier to work with the patch previously
> sent to remove that layer, but I suspect bad things may happen to
> sama5d4 users if they do not have that layer removed.
> 
> Cheers,
> peda
> 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH v3 2/3] drm/fb-cma-helper: expose more of fb cma guts
  2017-06-19  7:44 ` [PATCH v3 2/3] drm/fb-cma-helper: expose more of fb cma guts Peter Rosin
@ 2017-06-19 20:41   ` Boris Brezillon
  0 siblings, 0 replies; 7+ messages in thread
From: Boris Brezillon @ 2017-06-19 20:41 UTC (permalink / raw)
  To: Peter Rosin
  Cc: linux-kernel, David Airlie, Daniel Vetter, Jani Nikula, Sean Paul,
	dri-devel

Le Mon, 19 Jun 2017 09:44:25 +0200,
Peter Rosin <peda@axentia.se> a écrit :

> DRM drivers supporting clut may want a convenient way to only use
> non-default .gamma_set and .gamma_get ops in the drm_fb_helper_funcs
> in order to avoid the following
> 
> 	/*
> 	 * The driver really shouldn't advertise pseudo/directcolor
> 	 * visuals if it can't deal with the palette.
> 	 */
> 	if (WARN_ON(!fb_helper->funcs->gamma_set ||
> 		    !fb_helper->funcs->gamma_get))
> 		return -EINVAL;
> 
> warning in drm_fb_helper.c:setcolreg().

Did you read the FIXME in the struct doc [1]? Shouldn't we try to move
to this generic approach instead of exposing fb_cma internals? You
could probably mimic (or re-use) what's done in
drm_mode_gamma_set/get_ioctl() [2].

[1]http://elixir.free-electrons.com/linux/v4.12-rc6/source/include/drm/drm_fb_helper.h#L110
[2]http://elixir.free-electrons.com/linux/v4.12-rc6/source/drivers/gpu/drm/drm_color_mgmt.c#L214

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2017-06-19 20:41 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-06-19  7:44 [PATCH v3 0/3] drm: atmel-hlcdc: clut support Peter Rosin
2017-06-19  7:44 ` [PATCH v3 1/3] drm: atmel-hlcdc: add support for 8-bit color lookup table mode Peter Rosin
2017-06-19  7:44 ` [PATCH v3 2/3] drm/fb-cma-helper: expose more of fb cma guts Peter Rosin
2017-06-19 20:41   ` Boris Brezillon
2017-06-19  7:44 ` [PATCH v3 3/3] drm: atmel-hlcdc: add clut support for legacy fbdev Peter Rosin
2017-06-19  7:48 ` [PATCH v3 0/3] drm: atmel-hlcdc: clut support Boris Brezillon
2017-06-19 20:09 ` Boris Brezillon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox