summaryrefslogtreecommitdiff
path: root/recipes-graphics/mesa/mesa
diff options
context:
space:
mode:
authorDominik Sliwa <dominik.sliwa@toradex.com>2017-01-09 17:35:46 +0100
committerDominik Sliwa <dominik.sliwa@toradex.com>2017-01-11 21:59:33 +0100
commit6c22936df0c551961df61cc428e6590d2f8bbb6d (patch)
tree163ad22e51e402a02ac2fd98156f1db0f750d3a1 /recipes-graphics/mesa/mesa
parentd54c4ca13c2976671fb2e91b774ae1a94039715a (diff)
apalis-tk1-mainline: Move GPU acceleration support to mesa
Instead of patching every app, let's patch mesa to pretend that we have a normal GPU. based on: github.com/Gnurou/mesa/commits/renderonly Signed-off-by: Dominik Sliwa <dominik.sliwa@toradex.com> Acked-by: Marcel Ziswiler <marcel.ziswiler@toradex.com>
Diffstat (limited to 'recipes-graphics/mesa/mesa')
-rw-r--r--recipes-graphics/mesa/mesa/0001-gallium-add-renderonly-library.patch365
-rw-r--r--recipes-graphics/mesa/mesa/0004-gallium-add-tegra-support.patch514
-rw-r--r--recipes-graphics/mesa/mesa/0005-tegra-open-card1-instead-of-renderD128.patch29
-rw-r--r--recipes-graphics/mesa/mesa/0006-HACK-make-DRI-work-under-X.patch24
-rw-r--r--recipes-graphics/mesa/mesa/0008-remove-dependency-on-libdrm_tegra.patch48
5 files changed, 980 insertions, 0 deletions
diff --git a/recipes-graphics/mesa/mesa/0001-gallium-add-renderonly-library.patch b/recipes-graphics/mesa/mesa/0001-gallium-add-renderonly-library.patch
new file mode 100644
index 0000000..884f59b
--- /dev/null
+++ b/recipes-graphics/mesa/mesa/0001-gallium-add-renderonly-library.patch
@@ -0,0 +1,365 @@
+From f8bf6404b0133a355a58c72fb4381943a0022959 Mon Sep 17 00:00:00 2001
+From: Christian Gmeiner <christian.gmeiner@gmail.com>
+Date: Sun, 27 Nov 2016 22:27:18 +0100
+Subject: [PATCH 1/8] gallium: add renderonly library
+
+This a very lightweight library to add basic support for
+renderonly GPUs. It does all the magic regarding in/exporting
+buffers etc. This library will likely break android support and
+hopefully will get replaced with a better solution based on gbm2.
+
+Signed-off-by: Christian Gmeiner <christian.gmeiner@gmail.com>
+---
+ src/gallium/Automake.inc | 5 +
+ src/gallium/auxiliary/Makefile.am | 10 ++
+ src/gallium/auxiliary/Makefile.sources | 4 +
+ src/gallium/auxiliary/renderonly/renderonly.c | 199 ++++++++++++++++++++++++++
+ src/gallium/auxiliary/renderonly/renderonly.h | 81 +++++++++++
+ 5 files changed, 299 insertions(+)
+ create mode 100644 src/gallium/auxiliary/renderonly/renderonly.c
+ create mode 100644 src/gallium/auxiliary/renderonly/renderonly.h
+
+diff --git a/src/gallium/Automake.inc b/src/gallium/Automake.inc
+index 6fe2e22..6aadcb9 100644
+--- a/src/gallium/Automake.inc
++++ b/src/gallium/Automake.inc
+@@ -50,6 +50,11 @@ GALLIUM_COMMON_LIB_DEPS = \
+ $(PTHREAD_LIBS) \
+ $(DLOPEN_LIBS)
+
++if HAVE_LIBDRM
++GALLIUM_COMMON_LIB_DEPS += \
++ $(LIBDRM_LIBS)
++endif
++
+ GALLIUM_WINSYS_CFLAGS = \
+ -I$(top_srcdir)/src \
+ -I$(top_srcdir)/include \
+diff --git a/src/gallium/auxiliary/Makefile.am b/src/gallium/auxiliary/Makefile.am
+index 4a4a4fb..6b63cf1 100644
+--- a/src/gallium/auxiliary/Makefile.am
++++ b/src/gallium/auxiliary/Makefile.am
+@@ -20,6 +20,16 @@ libgallium_la_SOURCES = \
+ $(NIR_SOURCES) \
+ $(GENERATED_SOURCES)
+
++if HAVE_LIBDRM
++
++AM_CFLAGS += \
++ $(LIBDRM_CFLAGS)
++
++libgallium_la_SOURCES += \
++ $(RENDERONLY_SOURCES)
++
++endif
++
+ if HAVE_MESA_LLVM
+
+ AM_CFLAGS += \
+diff --git a/src/gallium/auxiliary/Makefile.sources b/src/gallium/auxiliary/Makefile.sources
+index 5d4fe30..8d3e4a9 100644
+--- a/src/gallium/auxiliary/Makefile.sources
++++ b/src/gallium/auxiliary/Makefile.sources
+@@ -435,3 +435,7 @@ GALLIVM_SOURCES := \
+ draw/draw_llvm_sample.c \
+ draw/draw_pt_fetch_shade_pipeline_llvm.c \
+ draw/draw_vs_llvm.c
++
++RENDERONLY_SOURCES := \
++ renderonly/renderonly.c \
++ renderonly/renderonly.h
+diff --git a/src/gallium/auxiliary/renderonly/renderonly.c b/src/gallium/auxiliary/renderonly/renderonly.c
+new file mode 100644
+index 0000000..c4ea784
+--- /dev/null
++++ b/src/gallium/auxiliary/renderonly/renderonly.c
+@@ -0,0 +1,199 @@
++/*
++ * Copyright (C) 2016 Christian Gmeiner <christian.gmeiner@gmail.com>
++ *
++ * 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:
++ * Christian Gmeiner <christian.gmeiner@gmail.com>
++ */
++
++#include "renderonly/renderonly.h"
++
++#include <errno.h>
++#include <fcntl.h>
++#include <stdio.h>
++#include <sys/ioctl.h>
++#include <xf86drm.h>
++
++#include "state_tracker/drm_driver.h"
++#include "pipe/p_screen.h"
++#include "util/u_memory.h"
++
++struct pipe_screen *
++renderonly_screen_create(int fd, const struct renderonly_ops *ops, void *priv)
++{
++ struct renderonly *ro;
++
++ ro = CALLOC_STRUCT(renderonly);
++ if (!ro)
++ return NULL;
++
++ ro->kms_fd = fd;
++ ro->ops = ops;
++ ro->priv = priv;
++
++ ro->screen = ops->create(ro);
++ if (!ro->screen)
++ goto cleanup;
++
++ return ro->screen;
++
++cleanup:
++ FREE(ro);
++
++ return NULL;
++}
++
++static bool
++use_kms_bumb_buffer(struct renderonly_scanout *scanout,
++ struct pipe_resource *rsc, struct renderonly *ro)
++{
++ struct winsys_handle handle;
++ int prime_fd, err;
++ struct drm_mode_create_dumb create_dumb = {
++ .width = rsc->width0,
++ .height = rsc->height0,
++ .bpp = 32,
++ };
++ struct drm_mode_destroy_dumb destroy_dumb = { };
++
++ /* create dumb buffer at scanout GPU */
++ err = ioctl(ro->kms_fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
++ if (err < 0) {
++ fprintf(stderr, "DRM_IOCTL_MODE_CREATE_DUMB failed: %s\n",
++ strerror(errno));
++ return false;
++ }
++
++ scanout->handle = create_dumb.handle;
++ scanout->stride = create_dumb.pitch;
++
++ /* export dumb buffer */
++ err = drmPrimeHandleToFD(ro->kms_fd, create_dumb.handle, O_CLOEXEC,
++ &prime_fd);
++ if (err < 0) {
++ fprintf(stderr, "failed to export dumb buffer: %s\n", strerror(errno));
++ goto out_free_dumb;
++ }
++
++ /* import dumb buffer */
++ handle.type = DRM_API_HANDLE_TYPE_FD;
++ handle.handle = prime_fd;
++ handle.stride = create_dumb.pitch;
++
++ scanout->prime = ro->screen->resource_from_handle(ro->screen, rsc,
++ &handle, PIPE_HANDLE_USAGE_READ_WRITE);
++
++ if (!scanout->prime) {
++ fprintf(stderr, "failed to create resource_from_handle: %s\n", strerror(errno));
++ goto out_free_dumb;
++ }
++
++ return true;
++
++out_free_dumb:
++ destroy_dumb.handle = scanout->handle;
++ ioctl(ro->kms_fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
++
++ return false;
++}
++
++static bool
++import_gpu_scanout(struct renderonly_scanout *scanout,
++ struct pipe_resource *rsc, struct renderonly *ro)
++{
++ struct pipe_screen *screen = ro->screen;
++ boolean status;
++ int fd, err;
++ struct winsys_handle handle = {
++ .type = DRM_API_HANDLE_TYPE_FD
++ };
++
++ status = screen->resource_get_handle(screen, rsc, &handle,
++ PIPE_HANDLE_USAGE_READ_WRITE);
++ if (!status)
++ return false;
++
++ scanout->stride = handle.stride;
++ fd = handle.handle;
++
++ err = drmPrimeFDToHandle(ro->kms_fd, fd, &scanout->handle);
++ close(fd);
++
++ if (err < 0) {
++ fprintf(stderr, "drmPrimeFDToHandle() failed: %s\n", strerror(errno));
++ return false;
++ }
++
++ if (ro->ops->tiling) {
++ err = ro->ops->tiling(ro->kms_fd, scanout->handle);
++ if (err < 0) {
++ fprintf(stderr, "failed to set tiling parameters: %s\n", strerror(errno));
++ close(scanout->handle);
++ return false;
++ }
++ }
++
++ return true;
++}
++
++struct renderonly_scanout *
++renderonly_scanout_for_resource(struct pipe_resource *rsc, struct renderonly *ro)
++{
++ struct renderonly_scanout *scanout;
++ bool ret;
++
++ scanout = CALLOC_STRUCT(renderonly_scanout);
++ if (!scanout)
++ return NULL;
++
++ if (ro->ops->intermediate_rendering)
++ ret = use_kms_bumb_buffer(scanout, rsc, ro);
++ else
++ ret = import_gpu_scanout(scanout, rsc, ro);
++
++ if (!ret) {
++ FREE(scanout);
++ scanout = NULL;
++ }
++
++ return scanout;
++}
++
++struct renderonly_scanout *
++renderonly_scanout_for_prime(struct pipe_resource *rsc, struct renderonly *ro)
++{
++ struct renderonly_scanout *scanout;
++
++ scanout = CALLOC_STRUCT(renderonly_scanout);
++ if (!scanout)
++ return NULL;
++
++ scanout->prime = rsc;
++
++ return scanout;
++}
++
++void
++renderonly_scanout_destroy(struct renderonly_scanout *scanout)
++{
++ close(scanout->handle);
++ FREE(scanout);
++}
+diff --git a/src/gallium/auxiliary/renderonly/renderonly.h b/src/gallium/auxiliary/renderonly/renderonly.h
+new file mode 100644
+index 0000000..5a98992
+--- /dev/null
++++ b/src/gallium/auxiliary/renderonly/renderonly.h
+@@ -0,0 +1,81 @@
++/*
++ * Copyright (C) 2016 Christian Gmeiner <christian.gmeiner@gmail.com>
++ *
++ * 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:
++ * Christian Gmeiner <christian.gmeiner@gmail.com>
++ */
++
++#ifndef RENDERONLY_H
++#define RENDERONLY_H
++
++#include <stdint.h>
++#include "state_tracker/drm_driver.h"
++#include "pipe/p_state.h"
++
++struct renderonly;
++
++struct renderonly_ops {
++ struct pipe_screen *(*create)(struct renderonly *ctx);
++ int (*tiling)(int fd, uint32_t handle);
++ bool intermediate_rendering;
++};
++
++struct renderonly {
++ int kms_fd;
++ const struct renderonly_ops *ops;
++ struct pipe_screen *screen;
++ void *priv;
++};
++
++struct pipe_screen *
++renderonly_screen_create(int fd, const struct renderonly_ops *ops, void *priv);
++
++struct renderonly_scanout {
++ uint32_t handle;
++ uint32_t stride;
++
++ struct pipe_resource *prime;
++};
++
++struct renderonly_scanout *
++renderonly_scanout_for_resource(struct pipe_resource *rsc, struct renderonly *ro);
++
++struct renderonly_scanout *
++renderonly_scanout_for_prime(struct pipe_resource *rsc, struct renderonly *ro);
++
++void
++renderonly_scanout_destroy(struct renderonly_scanout *scanout);
++
++static inline boolean
++renderonly_get_handle(struct renderonly_scanout *scanout,
++ struct winsys_handle *handle)
++{
++ if (!scanout)
++ return FALSE;
++
++ handle->handle = scanout->handle;
++ handle->stride = scanout->stride;
++
++ return TRUE;
++}
++
++#endif /* RENDERONLY_H_ */
+--
+2.9.3
+
diff --git a/recipes-graphics/mesa/mesa/0004-gallium-add-tegra-support.patch b/recipes-graphics/mesa/mesa/0004-gallium-add-tegra-support.patch
new file mode 100644
index 0000000..536d6cd
--- /dev/null
+++ b/recipes-graphics/mesa/mesa/0004-gallium-add-tegra-support.patch
@@ -0,0 +1,514 @@
+From 430452494ff5f3454290cb0c80f9d1dbd072ecff Mon Sep 17 00:00:00 2001
+From: Christian Gmeiner <christian.gmeiner@gmail.com>
+Date: Sun, 21 Aug 2016 22:10:12 +0200
+Subject: [PATCH 4/8] gallium: add tegra support
+
+Signed-off-by: Christian Gmeiner <christian.gmeiner@gmail.com>
+[acourbot@nvidia.com: port to latest branch]
+Signed-off-by: Alexandre Courbot <acourbot@nvidia.com>
+---
+ configure.ac | 13 ++++++-
+ src/gallium/Makefile.am | 4 +++
+ .../auxiliary/pipe-loader/pipe_loader_drm.c | 5 +++
+ src/gallium/auxiliary/target-helpers/drm_helper.h | 23 ++++++++++++
+ .../auxiliary/target-helpers/drm_helper_public.h | 3 ++
+ src/gallium/drivers/nouveau/nouveau_buffer.c | 3 ++
+ src/gallium/drivers/nouveau/nouveau_buffer.h | 2 ++
+ src/gallium/drivers/nouveau/nouveau_screen.h | 3 ++
+ src/gallium/drivers/nouveau/nv50/nv50_miptree.c | 3 ++
+ src/gallium/drivers/nouveau/nvc0/nvc0_resource.c | 18 ++++++++--
+ src/gallium/drivers/tegra/Automake.inc | 9 +++++
+ src/gallium/drivers/tegra/Makefile.am | 9 +++++
+ src/gallium/targets/dri/Makefile.am | 2 ++
+ src/gallium/targets/dri/target.c | 11 ++++++
+ .../winsys/nouveau/drm/nouveau_drm_public.h | 2 ++
+ .../winsys/nouveau/drm/nouveau_drm_winsys.c | 10 ++++++
+ src/gallium/winsys/tegra/drm/Android.mk | 33 +++++++++++++++++
+ src/gallium/winsys/tegra/drm/Makefile.am | 33 +++++++++++++++++
+ src/gallium/winsys/tegra/drm/Makefile.sources | 3 ++
+ src/gallium/winsys/tegra/drm/tegra_drm_public.h | 8 +++++
+ src/gallium/winsys/tegra/drm/tegra_drm_winsys.c | 41 ++++++++++++++++++++++
+ 21 files changed, 235 insertions(+), 3 deletions(-)
+ create mode 100644 src/gallium/drivers/tegra/Automake.inc
+ create mode 100644 src/gallium/drivers/tegra/Makefile.am
+ create mode 100644 src/gallium/winsys/tegra/drm/Android.mk
+ create mode 100644 src/gallium/winsys/tegra/drm/Makefile.am
+ create mode 100644 src/gallium/winsys/tegra/drm/Makefile.sources
+ create mode 100644 src/gallium/winsys/tegra/drm/tegra_drm_public.h
+ create mode 100644 src/gallium/winsys/tegra/drm/tegra_drm_winsys.c
+
+diff --git a/configure.ac b/configure.ac
+index c0f81ce..3d288e8 100644
+--- a/configure.ac
++++ b/configure.ac
+@@ -77,6 +77,7 @@ LIBDRM_NOUVEAU_REQUIRED=2.4.66
+ LIBDRM_NVVIEUX_REQUIRED=2.4.66
+ LIBDRM_NOUVEAU_REQUIRED=2.4.66
+ LIBDRM_FREEDRENO_REQUIRED=2.4.67
++LIBDRM_TEGRA_REQUIRED=2.4.68
+ DRI2PROTO_REQUIRED=2.6
+ DRI3PROTO_REQUIRED=1.0
+ PRESENTPROTO_REQUIRED=1.0
+@@ -1021,7 +1022,7 @@ GALLIUM_DRIVERS_DEFAULT="r300,r600,svga,swrast"
+ AC_ARG_WITH([gallium-drivers],
+ [AS_HELP_STRING([--with-gallium-drivers@<:@=DIRS...@:>@],
+ [comma delimited Gallium drivers list, e.g.
+- "i915,ilo,nouveau,r300,r600,radeonsi,freedreno,svga,swrast,vc4,virgl"
++ "i915,ilo,nouveau,r300,r600,radeonsi,freedreno,svga,swrast,vc4,virgl,tegra"
+ @<:@default=r300,r600,svga,swrast@:>@])],
+ [with_gallium_drivers="$withval"],
+ [with_gallium_drivers="$GALLIUM_DRIVERS_DEFAULT"])
+@@ -2491,6 +2492,13 @@ if test -n "$with_gallium_drivers"; then
+ require_libdrm "virgl"
+ require_basic_egl "virgl"
+ ;;
++ xtegra)
++ PKG_CHECK_MODULES([TEGRA], [libdrm_tegra >= $LIBDRM_TEGRA_REQUIRED])
++ HAVE_GALLIUM_TEGRA=yes
++ gallium_require_drm "tegra"
++ gallium_require_drm_loader
++ require_egl_drm "tegra"
++ ;;
+ *)
+ AC_MSG_ERROR([Unknown Gallium driver: $driver])
+ ;;
+@@ -2582,6 +2590,7 @@ AM_CONDITIONAL(HAVE_GALLIUM_VC4, test "x$HAVE_GALLIUM_VC4" = xyes)
+ "x$HAVE_GALLIUM_SWR" = xyes)
+ AM_CONDITIONAL(HAVE_GALLIUM_VC4, test "x$HAVE_GALLIUM_VC4" = xyes)
+ AM_CONDITIONAL(HAVE_GALLIUM_VIRGL, test "x$HAVE_GALLIUM_VIRGL" = xyes)
++AM_CONDITIONAL(HAVE_GALLIUM_TEGRA, test "x$HAVE_GALLIUM_TEGRA" = xyes)
+
+ AM_CONDITIONAL(HAVE_GALLIUM_STATIC_TARGETS, test "x$enable_shared_pipe_drivers" = xno)
+
+@@ -2732,6 +2741,7 @@ AC_CONFIG_FILES([Makefile
+ src/gallium/drivers/imx/Makefile
+ src/gallium/drivers/vc4/Makefile
+ src/gallium/drivers/virgl/Makefile
++ src/gallium/drivers/tegra/Makefile
+ src/gallium/state_trackers/clover/Makefile
+ src/gallium/state_trackers/dri/Makefile
+ src/gallium/state_trackers/glx/xlib/Makefile
+@@ -2776,6 +2786,7 @@ AC_CONFIG_FILES([Makefile
+ src/gallium/winsys/vc4/drm/Makefile
+ src/gallium/winsys/virgl/drm/Makefile
+ src/gallium/winsys/virgl/vtest/Makefile
++ src/gallium/winsys/tegra/drm/Makefile
+ src/gbm/Makefile
+ src/gbm/main/gbm.pc
+ src/glx/Makefile
+diff --git a/src/gallium/Makefile.am b/src/gallium/Makefile.am
+index f910f31..0f887d2 100644
+--- a/src/gallium/Makefile.am
++++ b/src/gallium/Makefile.am
+@@ -100,6 +100,10 @@ if HAVE_GALLIUM_VIRGL
+ SUBDIRS += drivers/virgl winsys/virgl/drm winsys/virgl/vtest
+ endif
+
++if HAVE_GALLIUM_TEGRA
++SUBDIRS += drivers/tegra winsys/tegra/drm
++endif
++
+ ## the sw winsys'
+ SUBDIRS += winsys/sw/null
+
+diff --git a/src/gallium/auxiliary/pipe-loader/pipe_loader_drm.c b/src/gallium/auxiliary/pipe-loader/pipe_loader_drm.c
+index 6c89fe5..ff1166c 100644
+--- a/src/gallium/auxiliary/pipe-loader/pipe_loader_drm.c
++++ b/src/gallium/auxiliary/pipe-loader/pipe_loader_drm.c
+@@ -163,6 +163,12 @@ static const struct drm_driver_descriptor driver_descriptors[] = {
+ .create_screen = pipe_vc4_create_screen,
+ .configuration = configuration_query,
+ },
++ {
++ .name = "tegra",
++ .driver_name = "tegra",
++ .create_screen = pipe_tegra_create_screen,
++ .configuration = configuration_query,
++ },
+ };
+ #endif
+
+diff --git a/src/gallium/auxiliary/target-helpers/drm_helper.h b/src/gallium/auxiliary/target-helpers/drm_helper.h
+index fbb4ce4..22932d3 100644
+--- a/src/gallium/auxiliary/target-helpers/drm_helper.h
++++ b/src/gallium/auxiliary/target-helpers/drm_helper.h
+@@ -312,4 +312,27 @@ pipe_imx_drm_create_screen(int fd)
+
+ #endif
+
++#ifdef GALLIUM_TEGRA
++#include "tegra/drm/tegra_drm_public.h"
++
++struct pipe_screen *
++pipe_tegra_create_screen(int fd)
++{
++ struct pipe_screen *screen;
++
++ screen = tegra_drm_screen_create(fd);
++ return screen ? debug_screen_wrap(screen) : NULL;
++}
++
++#else
++
++struct pipe_screen *
++pipe_tegra_create_screen(int fd)
++{
++ fprintf(stderr, "tegra: driver missing\n");
++ return NULL;
++}
++
++#endif
++
+ #endif /* DRM_HELPER_H */
+diff --git a/src/gallium/auxiliary/target-helpers/drm_helper_public.h b/src/gallium/auxiliary/target-helpers/drm_helper_public.h
+index bc12b21..5525bdf 100644
+--- a/src/gallium/auxiliary/target-helpers/drm_helper_public.h
++++ b/src/gallium/auxiliary/target-helpers/drm_helper_public.h
+@@ -40,4 +40,7 @@ pipe_etna_create_screen(int fd);
+ struct pipe_screen *
+ pipe_imx_drm_create_screen(int fd);
+
++struct pipe_screen *
++pipe_tegra_create_screen(int fd);
++
+ #endif /* _DRM_HELPER_PUBLIC_H */
+diff --git a/src/gallium/drivers/nouveau/nouveau_buffer.c b/src/gallium/drivers/nouveau/nouveau_buffer.c
+index 17052b2..95dcaad 100644
+--- a/src/gallium/drivers/nouveau/nouveau_buffer.c
++++ b/src/gallium/drivers/nouveau/nouveau_buffer.c
+@@ -128,6 +128,9 @@ nouveau_buffer_destroy(struct pipe_screen *pscreen,
+
+ util_range_destroy(&res->valid_buffer_range);
+
++ if (res->scanout)
++ renderonly_scanout_destroy(res->scanout);
++
+ FREE(res);
+
+ NOUVEAU_DRV_STAT(nouveau_screen(pscreen), buf_obj_current_count, -1);
+diff --git a/src/gallium/drivers/nouveau/nouveau_buffer.h b/src/gallium/drivers/nouveau/nouveau_buffer.h
+index 3a33fae..c01fda4 100644
+--- a/src/gallium/drivers/nouveau/nouveau_buffer.h
++++ b/src/gallium/drivers/nouveau/nouveau_buffer.h
+@@ -50,6 +50,8 @@ struct nv04_resource {
+
+ /* buffer range that has been initialized */
+ struct util_range valid_buffer_range;
++
++ struct renderonly_scanout *scanout;
+ };
+
+ void
+diff --git a/src/gallium/drivers/nouveau/nouveau_screen.h b/src/gallium/drivers/nouveau/nouveau_screen.h
+index 28c4760..e0ec614 100644
+--- a/src/gallium/drivers/nouveau/nouveau_screen.h
++++ b/src/gallium/drivers/nouveau/nouveau_screen.h
+@@ -2,6 +2,7 @@
+ #define __NOUVEAU_SCREEN_H__
+
+ #include "pipe/p_screen.h"
++#include "renderonly/renderonly.h"
+ #include "util/u_memory.h"
+
+ #ifdef DEBUG
+@@ -59,6 +60,8 @@ struct nouveau_screen {
+ unsigned profiles_present;
+ } firmware_info;
+
++ struct renderonly *ro;
++
+ #ifdef NOUVEAU_ENABLE_DRIVER_STATISTICS
+ union {
+ uint64_t v[29];
+diff --git a/src/gallium/drivers/nouveau/nv50/nv50_miptree.c b/src/gallium/drivers/nouveau/nv50/nv50_miptree.c
+index f2e304f..29e95a6 100644
+--- a/src/gallium/drivers/nouveau/nv50/nv50_miptree.c
++++ b/src/gallium/drivers/nouveau/nv50/nv50_miptree.c
+@@ -189,6 +189,9 @@ nv50_miptree_get_handle(struct pipe_screen *pscreen,
+ if (!mt || !mt->base.bo)
+ return false;
+
++ if (renderonly_get_handle(mt->base.scanout, whandle))
++ return TRUE;
++
+ stride = mt->level[0].pitch;
+
+ return nouveau_screen_bo_get_handle(pscreen,
+diff --git a/src/gallium/drivers/nouveau/nvc0/nvc0_resource.c b/src/gallium/drivers/nouveau/nvc0/nvc0_resource.c
+index 9bafe3d..adfd61c 100644
+--- a/src/gallium/drivers/nouveau/nvc0/nvc0_resource.c
++++ b/src/gallium/drivers/nouveau/nvc0/nvc0_resource.c
+@@ -8,12 +8,26 @@ static struct pipe_resource *
+ nvc0_resource_create(struct pipe_screen *screen,
+ const struct pipe_resource *templ)
+ {
++ struct nouveau_screen *scr = nouveau_screen(screen);
++ struct pipe_resource *pres;
++
+ switch (templ->target) {
+ case PIPE_BUFFER:
+- return nouveau_buffer_create(screen, templ);
++ pres = nouveau_buffer_create(screen, templ);
++ break;
+ default:
+- return nvc0_miptree_create(screen, templ);
++ pres = nvc0_miptree_create(screen, templ);
++ break;
+ }
++
++ if (pres) {
++ struct nv04_resource *res = nv04_resource(pres);
++
++ if (templ->bind & PIPE_BIND_SCANOUT)
++ res->scanout = renderonly_scanout_for_resource(pres, scr->ro);
++ }
++
++ return pres;
+ }
+
+ static struct pipe_resource *
+diff --git a/src/gallium/drivers/tegra/Automake.inc b/src/gallium/drivers/tegra/Automake.inc
+new file mode 100644
+index 0000000..89e0441
+--- /dev/null
++++ b/src/gallium/drivers/tegra/Automake.inc
+@@ -0,0 +1,9 @@
++if HAVE_GALLIUM_TEGRA
++
++TARGET_DRIVERS += tegra
++TARGET_CPPFLAGS += -DGALLIUM_TEGRA
++TARGET_LIB_DEPS += \
++ $(top_builddir)/src/gallium/winsys/tegra/drm/libtegradrm.la \
++ $(LIBDRM_LIBS)
++
++endif
+diff --git a/src/gallium/drivers/tegra/Makefile.am b/src/gallium/drivers/tegra/Makefile.am
+new file mode 100644
+index 0000000..30f4689
+--- /dev/null
++++ b/src/gallium/drivers/tegra/Makefile.am
+@@ -0,0 +1,9 @@
++include $(top_srcdir)/src/gallium/Automake.inc
++
++AM_CPPFLAGS = \
++ $(GALLIUM_CFLAGS) \
++ $(TEGRA_CFLAGS)
++
++noinst_LTLIBRARIES = libtegra.la
++
++libtegra_la_SOURCES =
+diff --git a/src/gallium/targets/dri/Makefile.am b/src/gallium/targets/dri/Makefile.am
+index 06ade45..ed412d7 100644
+--- a/src/gallium/targets/dri/Makefile.am
++++ b/src/gallium/targets/dri/Makefile.am
+@@ -91,6 +91,8 @@ include $(top_srcdir)/src/gallium/drivers/vc4/Automake.inc
+
+ include $(top_srcdir)/src/gallium/drivers/virgl/Automake.inc
+
++include $(top_srcdir)/src/gallium/drivers/tegra/Automake.inc
++
+ include $(top_srcdir)/src/gallium/drivers/softpipe/Automake.inc
+ include $(top_srcdir)/src/gallium/drivers/llvmpipe/Automake.inc
+ include $(top_srcdir)/src/gallium/drivers/swr/Automake.inc
+diff --git a/src/gallium/targets/dri/target.c b/src/gallium/targets/dri/target.c
+index 01532e2..126cadf 100644
+--- a/src/gallium/targets/dri/target.c
++++ b/src/gallium/targets/dri/target.c
+@@ -162,3 +162,14 @@ PUBLIC const __DRIextension **__driDriverGetExtensions_i965(void)
+ }
+ #endif
+ #endif
++
++#if defined(GALLIUM_TEGRA)
++
++const __DRIextension **__driDriverGetExtensions_tegra(void);
++
++PUBLIC const __DRIextension **__driDriverGetExtensions_tegra(void)
++{
++ globalDriverAPI = &galliumdrm_driver_api;
++ return galliumdrm_driver_extensions;
++}
++#endif
+diff --git a/src/gallium/winsys/nouveau/drm/nouveau_drm_public.h b/src/gallium/winsys/nouveau/drm/nouveau_drm_public.h
+index 67b7c44..455ea77 100644
+--- a/src/gallium/winsys/nouveau/drm/nouveau_drm_public.h
++++ b/src/gallium/winsys/nouveau/drm/nouveau_drm_public.h
+@@ -3,7 +3,9 @@
+ #define __NOUVEAU_DRM_PUBLIC_H__
+
+ struct pipe_screen;
++struct renderonly;
+
+ struct pipe_screen *nouveau_drm_screen_create(int drmFD);
++struct pipe_screen *nouveau_drm_screen_create_renderonly(int fd, struct renderonly *ro);
+
+ #endif
+diff --git a/src/gallium/winsys/nouveau/drm/nouveau_drm_winsys.c b/src/gallium/winsys/nouveau/drm/nouveau_drm_winsys.c
+index cc9dfa7..acbd3fe 100644
+--- a/src/gallium/winsys/nouveau/drm/nouveau_drm_winsys.c
++++ b/src/gallium/winsys/nouveau/drm/nouveau_drm_winsys.c
+@@ -157,3 +157,13 @@ err:
+ pipe_mutex_unlock(nouveau_screen_mutex);
+ return NULL;
+ }
++
++struct pipe_screen *nouveau_drm_screen_create_renderonly(int fd, struct renderonly *ro)
++{
++ struct nouveau_screen *screen = nouveau_drm_screen_create(fd);
++
++ if (screen)
++ screen->ro = ro;
++
++ return screen;
++}
+diff --git a/src/gallium/winsys/tegra/drm/Android.mk b/src/gallium/winsys/tegra/drm/Android.mk
+new file mode 100644
+index 0000000..a48dca48
+--- /dev/null
++++ b/src/gallium/winsys/tegra/drm/Android.mk
+@@ -0,0 +1,33 @@
++# Copyright (C) 2014 Emil Velikov <emil.l.velikov@gmail.com>
++#
++# 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 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.
++
++LOCAL_PATH := $(call my-dir)
++
++# get C_SOURCES
++include $(LOCAL_PATH)/Makefile.sources
++
++include $(CLEAR_VARS)
++
++LOCAL_SRC_FILES := $(C_SOURCES)
++
++LOCAL_MODULE := libmesa_winsys_tegra
++
++include $(GALLIUM_COMMON_MK)
++include $(BUILD_STATIC_LIBRARY)
+diff --git a/src/gallium/winsys/tegra/drm/Makefile.am b/src/gallium/winsys/tegra/drm/Makefile.am
+new file mode 100644
+index 0000000..39068fe
+--- /dev/null
++++ b/src/gallium/winsys/tegra/drm/Makefile.am
+@@ -0,0 +1,33 @@
++# Copyright © 2012 Intel Corporation
++#
++# 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.
++
++include Makefile.sources
++include $(top_srcdir)/src/gallium/Automake.inc
++
++AM_CFLAGS = \
++ -I$(top_srcdir)/src/gallium/drivers \
++ $(GALLIUM_WINSYS_CFLAGS) \
++ $(TEGRA_CFLAGS)
++
++noinst_LTLIBRARIES = libtegradrm.la
++
++libtegradrm_la_SOURCES = $(C_SOURCES)
+diff --git a/src/gallium/winsys/tegra/drm/Makefile.sources b/src/gallium/winsys/tegra/drm/Makefile.sources
+new file mode 100644
+index 0000000..29a0edc
+--- /dev/null
++++ b/src/gallium/winsys/tegra/drm/Makefile.sources
+@@ -0,0 +1,3 @@
++C_SOURCES := \
++ tegra_drm_public.h \
++ tegra_drm_winsys.c
+diff --git a/src/gallium/winsys/tegra/drm/tegra_drm_public.h b/src/gallium/winsys/tegra/drm/tegra_drm_public.h
+new file mode 100644
+index 0000000..813268f
+--- /dev/null
++++ b/src/gallium/winsys/tegra/drm/tegra_drm_public.h
+@@ -0,0 +1,8 @@
++#ifndef __TEGRA_DRM_PUBLIC_H__
++#define __TEGRA_DRM_PUBLIC_H__
++
++struct pipe_screen;
++
++struct pipe_screen *tegra_drm_screen_create(int drmFD);
++
++#endif /* __TEGRA_DRM_PUBLIC_H__ */
+diff --git a/src/gallium/winsys/tegra/drm/tegra_drm_winsys.c b/src/gallium/winsys/tegra/drm/tegra_drm_winsys.c
+new file mode 100644
+index 0000000..361370d
+--- /dev/null
++++ b/src/gallium/winsys/tegra/drm/tegra_drm_winsys.c
+@@ -0,0 +1,41 @@
++#include "tegra_drm_public.h"
++
++#include "renderonly/renderonly.h"
++#include "../winsys/nouveau/drm/nouveau_drm_public.h"
++
++#include <fcntl.h>
++#include <libdrm/tegra_drm.h>
++#include <xf86drm.h>
++
++static struct pipe_screen *
++tegra_create(struct renderonly *ro)
++{
++ int fd = open("/dev/dri/renderD128", O_RDWR | O_CLOEXEC);
++
++ if (fd == -1)
++ return NULL;
++
++ return nouveau_drm_screen_create_renderonly(fd, ro);
++}
++
++static int
++tegra_tiling(int fd, uint32_t handle)
++{
++ struct drm_tegra_gem_set_tiling args = {
++ .handle = handle,
++ .mode = DRM_TEGRA_GEM_TILING_MODE_BLOCK,
++ .value = 4
++ };
++
++ return drmIoctl(fd, DRM_IOCTL_TEGRA_GEM_SET_TILING, &args);
++}
++
++static const struct renderonly_ops ro_ops = {
++ .create = tegra_create,
++ .tiling = tegra_tiling
++};
++
++struct pipe_screen *tegra_drm_screen_create(int fd)
++{
++ return renderonly_screen_create(fd, &ro_ops, NULL);
++}
+--
+2.9.3
+
diff --git a/recipes-graphics/mesa/mesa/0005-tegra-open-card1-instead-of-renderD128.patch b/recipes-graphics/mesa/mesa/0005-tegra-open-card1-instead-of-renderD128.patch
new file mode 100644
index 0000000..14a867a
--- /dev/null
+++ b/recipes-graphics/mesa/mesa/0005-tegra-open-card1-instead-of-renderD128.patch
@@ -0,0 +1,29 @@
+From a97f868a806ab4f1aaab88c72d4b55bfb18cc3cc Mon Sep 17 00:00:00 2001
+From: Alexandre Courbot <acourbot@nvidia.com>
+Date: Fri, 9 Dec 2016 17:04:16 +0900
+Subject: [PATCH 5/8] tegra: open card1 instead of renderD128
+
+Mesa wants to perform a FLINK ioctl to share GPU buffers. This ioctl is
+not valid on render-nodes.
+
+This fixes rendering with Weston, but GLX is still broken.
+---
+ src/gallium/winsys/tegra/drm/tegra_drm_winsys.c | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git a/src/gallium/winsys/tegra/drm/tegra_drm_winsys.c b/src/gallium/winsys/tegra/drm/tegra_drm_winsys.c
+index 361370d..e213942 100644
+--- a/src/gallium/winsys/tegra/drm/tegra_drm_winsys.c
++++ b/src/gallium/winsys/tegra/drm/tegra_drm_winsys.c
+@@ -10,7 +10,7 @@
+ static struct pipe_screen *
+ tegra_create(struct renderonly *ro)
+ {
+- int fd = open("/dev/dri/renderD128", O_RDWR | O_CLOEXEC);
++ int fd = open("/dev/dri/card1", O_RDWR | O_CLOEXEC);
+
+ if (fd == -1)
+ return NULL;
+--
+2.9.3
+
diff --git a/recipes-graphics/mesa/mesa/0006-HACK-make-DRI-work-under-X.patch b/recipes-graphics/mesa/mesa/0006-HACK-make-DRI-work-under-X.patch
new file mode 100644
index 0000000..ecafa3f
--- /dev/null
+++ b/recipes-graphics/mesa/mesa/0006-HACK-make-DRI-work-under-X.patch
@@ -0,0 +1,24 @@
+From bb572a449b8c168dbe6d723692e4c1c8801f15b8 Mon Sep 17 00:00:00 2001
+From: Alexandre Courbot <acourbot@nvidia.com>
+Date: Mon, 11 Jul 2016 14:44:59 +0900
+Subject: [PATCH 6/8] [HACK] make DRI work under X
+
+---
+ src/loader/loader_dri3_helper.c | 1 +
+ 1 file changed, 1 insertion(+)
+
+diff --git a/src/loader/loader_dri3_helper.c b/src/loader/loader_dri3_helper.c
+index ca02e97..568a1c7 100644
+--- a/src/loader/loader_dri3_helper.c
++++ b/src/loader/loader_dri3_helper.c
+@@ -145,6 +145,7 @@ loader_dri3_drawable_init(xcb_connection_t *conn,
+ draw->drawable = drawable;
+ draw->dri_screen = dri_screen;
+ draw->is_different_gpu = is_different_gpu;
++ draw->is_different_gpu = 1;
+
+ draw->have_back = 0;
+ draw->have_fake_front = 0;
+--
+2.9.3
+
diff --git a/recipes-graphics/mesa/mesa/0008-remove-dependency-on-libdrm_tegra.patch b/recipes-graphics/mesa/mesa/0008-remove-dependency-on-libdrm_tegra.patch
new file mode 100644
index 0000000..0b6d6e9
--- /dev/null
+++ b/recipes-graphics/mesa/mesa/0008-remove-dependency-on-libdrm_tegra.patch
@@ -0,0 +1,48 @@
+From d2716d25609865bb31bb270ee2fdac3dec5047c6 Mon Sep 17 00:00:00 2001
+From: Alexandre Courbot <acourbot@nvidia.com>
+Date: Tue, 20 Dec 2016 15:20:20 +0900
+Subject: [PATCH 8/8] remove dependency on libdrm_tegra
+
+Functions provided by this library are not required as we perform ioctls
+directly.
+---
+ configure.ac | 2 --
+ src/gallium/winsys/tegra/drm/Makefile.am | 2 +-
+ 2 files changed, 1 insertion(+), 3 deletions(-)
+
+diff --git a/configure.ac b/configure.ac
+index 8a7d833..7df9674 100644
+--- a/configure.ac
++++ b/configure.ac
+@@ -77,7 +77,6 @@ LIBDRM_NOUVEAU_REQUIRED=2.4.66
+ LIBDRM_NVVIEUX_REQUIRED=2.4.66
+ LIBDRM_NOUVEAU_REQUIRED=2.4.66
+ LIBDRM_FREEDRENO_REQUIRED=2.4.67
+-LIBDRM_TEGRA_REQUIRED=2.4.68
+ DRI2PROTO_REQUIRED=2.6
+ DRI3PROTO_REQUIRED=1.0
+ PRESENTPROTO_REQUIRED=1.0
+@@ -2493,7 +2492,6 @@ if test -n "$with_gallium_drivers"; then
+ require_basic_egl "virgl"
+ ;;
+ xtegra)
+- PKG_CHECK_MODULES([TEGRA], [libdrm_tegra >= $LIBDRM_TEGRA_REQUIRED])
+ HAVE_GALLIUM_TEGRA=yes
+ gallium_require_drm "tegra"
+ gallium_require_drm_loader
+diff --git a/src/gallium/winsys/tegra/drm/Makefile.am b/src/gallium/winsys/tegra/drm/Makefile.am
+index 39068fe..eada86a 100644
+--- a/src/gallium/winsys/tegra/drm/Makefile.am
++++ b/src/gallium/winsys/tegra/drm/Makefile.am
+@@ -26,7 +26,7 @@ include $(top_srcdir)/src/gallium/Automake.inc
+ AM_CFLAGS = \
+ -I$(top_srcdir)/src/gallium/drivers \
+ $(GALLIUM_WINSYS_CFLAGS) \
+- $(TEGRA_CFLAGS)
++ $(LIBDRM_CFLAGS)
+
+ noinst_LTLIBRARIES = libtegradrm.la
+
+--
+2.9.3
+