kms_getfb.c 6.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/*
 * Copyright © 2013 Intel Corporation
 * Copyright © 2018 Collabora, Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Authors:
 *    Daniel Vetter <daniel.vetter@ffwll.ch>
 *    Daniel Stone <daniels@collabora.com>
 *
 */

#include "igt.h"
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <inttypes.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include "drm.h"
#include "drm_fourcc.h"

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
static bool has_getfb_iface(int fd)
{
	struct drm_mode_fb_cmd arg = { };
	int err;

	err = 0;
	if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &arg))
		err = -errno;
	switch (err) {
	case -ENOTTY: /* ioctl unrecognised (kernel too old) */
	case -ENOTSUP: /* driver doesn't support KMS */
		return false;
	default:
		return true;
	}
}

static bool has_addfb2_iface(int fd)
{
	struct drm_mode_fb_cmd2 arg = { };
	int err;

	err = 0;
	if (drmIoctl(fd, DRM_IOCTL_MODE_ADDFB2, &arg))
		err = -errno;
	switch (err) {
	case -ENOTTY: /* ioctl unrecognised (kernel too old) */
	case -ENOTSUP: /* driver doesn't support KMS */
		return false;
	default:
		return true;
	}
}

77 78 79 80 81 82 83 84 85 86 87 88 89 90
static void get_ccs_fb(int fd, struct drm_mode_fb_cmd2 *ret)
{
	struct drm_mode_fb_cmd2 add = {
		.width = 1024,
		.height = 1024,
		.pixel_format = DRM_FORMAT_XRGB8888,
		.flags = DRM_MODE_FB_MODIFIERS,
		.modifier = {
			I915_FORMAT_MOD_Y_TILED_CCS,
			I915_FORMAT_MOD_Y_TILED_CCS,
		},
	};
	int size;

91
	igt_require(has_addfb2_iface(fd));
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
	igt_require_intel(fd);

	/* An explanation of the magic numbers can be found in kms_ccs.c. */
	add.pitches[0] = ALIGN(add.width * 4, 128);
	add.offsets[1] = add.pitches[0] * ALIGN(add.height, 32);
	add.pitches[1] = ALIGN(ALIGN(add.width * 4, 32) / 32, 128);

	size = add.offsets[1];
	size += add.pitches[1] * ALIGN(ALIGN(add.height, 16) / 16, 32);

	add.handles[0] = gem_create(fd, size);
	igt_require(add.handles[0] != 0);
	add.handles[1] = add.handles[0];

	if (drmIoctl(fd, DRM_IOCTL_MODE_ADDFB2, &add) == 0)
		*ret = add;
	else
		gem_close(fd, add.handles[0]);
}

112 113 114 115 116 117 118
/**
 * Find and return an arbitrary valid property ID.
 */
static uint32_t get_any_prop_id(int fd)
{
	igt_display_t display;

119
	igt_display_require(&display, fd);
120 121 122 123 124 125 126 127 128
	for (int i = 0; i < display.n_outputs; i++) {
		igt_output_t *output = &display.outputs[i];
		if (output->props[IGT_CONNECTOR_DPMS] != 0)
			return output->props[IGT_CONNECTOR_DPMS];
	}

	return 0;
}

129 130 131 132 133 134 135 136 137 138 139
static void test_handle_input(int fd)
{
	struct drm_mode_fb_cmd2 add = {};

	igt_fixture {
		add.width = 1024;
		add.height = 1024;
		add.pixel_format = DRM_FORMAT_XRGB8888;
		add.pitches[0] = 1024*4;
		add.handles[0] = igt_create_bo_with_dimensions(fd, 1024, 1024,
			DRM_FORMAT_XRGB8888, 0, 0, NULL, NULL, NULL);
140
		igt_require(add.handles[0] != 0);
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
		do_ioctl(fd, DRM_IOCTL_MODE_ADDFB2, &add);
	}

	igt_subtest("getfb-handle-zero") {
		struct drm_mode_fb_cmd get = { .fb_id = 0 };
		do_ioctl_err(fd, DRM_IOCTL_MODE_GETFB, &get, ENOENT);
	}

	igt_subtest("getfb-handle-valid") {
		struct drm_mode_fb_cmd get = { .fb_id = add.fb_id };
		do_ioctl(fd, DRM_IOCTL_MODE_GETFB, &get);
		igt_assert_neq_u32(get.handle, 0);
		igt_assert_eq_u32(get.width, add.width);
		igt_assert_eq_u32(get.height, add.height);
		igt_assert_eq_u32(get.pitch, add.pitches[0]);
		igt_assert_eq_u32(get.depth, 24);
		igt_assert_eq_u32(get.bpp, 32);
		gem_close(fd, get.handle);
	}

	igt_subtest("getfb-handle-closed") {
		struct drm_mode_fb_cmd get = { .fb_id = add.fb_id };
		do_ioctl(fd, DRM_IOCTL_MODE_RMFB, &add.fb_id);
		do_ioctl_err(fd, DRM_IOCTL_MODE_GETFB, &get, ENOENT);
	}

	igt_subtest("getfb-handle-not-fb") {
168 169
		struct drm_mode_fb_cmd get = { .fb_id = get_any_prop_id(fd) };
		igt_require(get.fb_id > 0);
170 171
		do_ioctl_err(fd, DRM_IOCTL_MODE_GETFB, &get, ENOENT);
	}
172 173 174

	igt_fixture
		gem_close(fd, add.handles[0]);
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
}

static void test_duplicate_handles(int fd)
{
	struct drm_mode_fb_cmd2 add = {};

	igt_fixture {
		add.width = 1024;
		add.height = 1024;
		add.pixel_format = DRM_FORMAT_XRGB8888;
		add.pitches[0] = 1024*4;
		add.handles[0] = igt_create_bo_with_dimensions(fd, 1024, 1024,
			DRM_FORMAT_XRGB8888, 0, 0, NULL, NULL, NULL);
		igt_assert(add.handles[0]);
		do_ioctl(fd, DRM_IOCTL_MODE_ADDFB2, &add);
	}

	igt_subtest("getfb-addfb-different-handles") {
		struct drm_mode_fb_cmd get = { .fb_id = add.fb_id };

		do_ioctl(fd, DRM_IOCTL_MODE_GETFB, &get);
		igt_assert_neq_u32(get.handle, add.handles[0]);
		gem_close(fd, get.handle);
	}

	igt_subtest("getfb-repeated-different-handles") {
		struct drm_mode_fb_cmd get1 = { .fb_id = add.fb_id };
		struct drm_mode_fb_cmd get2 = { .fb_id = add.fb_id };

		do_ioctl(fd, DRM_IOCTL_MODE_GETFB, &get1);
		do_ioctl(fd, DRM_IOCTL_MODE_GETFB, &get2);
		igt_assert_neq_u32(get1.handle, get2.handle);

		gem_close(fd, get1.handle);
		gem_close(fd, get2.handle);
	}

212 213 214 215 216 217 218 219 220 221 222 223 224
	igt_subtest("getfb-reject-ccs") {
		struct drm_mode_fb_cmd2 add_ccs = { };
		struct drm_mode_fb_cmd get = { };

		get_ccs_fb(fd, &add_ccs);
		igt_require(add_ccs.handles[0] != 0);
		get.fb_id = add_ccs.fb_id;
		do_ioctl_err(fd, DRM_IOCTL_MODE_GETFB, &get, EINVAL);

		do_ioctl(fd, DRM_IOCTL_MODE_RMFB, &add_ccs.fb_id);
		gem_close(fd, add_ccs.handles[0]);
	}

225 226 227 228 229 230 231 232 233 234
	igt_fixture {
		do_ioctl(fd, DRM_IOCTL_MODE_RMFB, &add.fb_id);
		gem_close(fd, add.handles[0]);
	}
}

igt_main
{
	int fd;

235
	igt_fixture {
236
		fd = drm_open_driver_master(DRIVER_ANY);
237 238
		igt_require(has_getfb_iface(fd));
	}
239

240 241
	igt_subtest_group
		test_handle_input(fd);
242

243 244
	igt_subtest_group
		test_duplicate_handles(fd);
245 246 247 248

	igt_fixture
		close(fd);
}