| 1 |
/* |
| 2 |
* Copyright (C) 2016 Christian Gmeiner <[email protected]> |
| 3 |
* Copyright (C) 2017 Broadcom |
| 4 |
* |
| 5 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 6 |
* copy of this software and associated documentation files (the "Software"), |
| 7 |
* to deal in the Software without restriction, including without limitation |
| 8 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following conditions: |
| 11 |
* |
| 12 |
* The above copyright notice and this permission notice (including the next |
| 13 |
* paragraph) shall be included in all copies or substantial portions of the |
| 14 |
* Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 |
* SOFTWARE. |
| 23 |
*/ |
| 24 |
|
| 25 |
#include <fcntl.h> |
| 26 |
#include <unistd.h> |
| 27 |
|
| 28 |
#include "kmsro_drm_public.h" |
| 29 |
#include "v3d/drm/v3d_drm_public.h" |
| 30 |
#include "vc4/drm/vc4_drm_public.h" |
| 31 |
#include "etnaviv/drm/etnaviv_drm_public.h" |
| 32 |
#include "freedreno/drm/freedreno_drm_public.h" |
| 33 |
#include "panfrost/drm/panfrost_drm_public.h" |
| 34 |
#include "lima/drm/lima_drm_public.h" |
| 35 |
#include "asahi/drm/asahi_drm_public.h" |
| 36 |
#include "zink/drm/zink_drm_public.h" |
| 37 |
#include "xf86drm.h" |
| 38 |
|
| 39 |
#include "pipe/p_screen.h" |
| 40 |
#include "pipe-loader/pipe_loader.h" |
| 41 |
#include "renderonly/renderonly.h" |
| 42 |
#include "util/u_memory.h" |
| 43 |
|
| 44 |
#include "loader.h" |
| 45 |
|
| 46 |
static void kmsro_ro_destroy(struct renderonly *ro) |
| 47 |
{ |
| 48 |
if (ro->gpu_fd >= 0) |
| 49 |
close(ro->gpu_fd); |
| 50 |
|
| 51 |
util_sparse_array_finish(&ro->bo_map); |
| 52 |
|
| 53 |
FREE(ro); |
| 54 |
} |
| 55 |
|
| 56 |
struct pipe_screen *kmsro_drm_screen_create(int kms_fd, |
| 57 |
const struct pipe_screen_config *config) |
| 58 |
{ |
| 59 |
struct pipe_screen *screen = NULL; |
| 60 |
char *render_dev_name = NULL; |
| 61 |
int *gpu_fds = NULL; |
| 62 |
unsigned int n_devices = 0; |
| 63 |
|
| 64 |
gpu_fds = pipe_loader_get_compatible_render_capable_device_fds(kms_fd, &n_devices); |
| 65 |
if (n_devices == 0) { |
| 66 |
goto out; |
| 67 |
} |
| 68 |
|
| 69 |
for (unsigned int i = 0; i < n_devices; i++) { |
| 70 |
struct renderonly *ro = CALLOC_STRUCT(renderonly); |
| 71 |
|
| 72 |
if (!ro) |
| 73 |
goto out; |
| 74 |
|
| 75 |
ro->kms_fd = kms_fd; |
| 76 |
ro->gpu_fd = dup(gpu_fds[i]); |
| 77 |
|
| 78 |
render_dev_name = loader_get_kernel_driver_name(ro->gpu_fd); |
| 79 |
if (!render_dev_name) { |
| 80 |
close(ro->gpu_fd); |
| 81 |
FREE(ro); |
| 82 |
goto out; |
| 83 |
} |
| 84 |
|
| 85 |
ro->destroy = kmsro_ro_destroy; |
| 86 |
util_sparse_array_init(&ro->bo_map, sizeof(struct renderonly_scanout), 64); |
| 87 |
simple_mtx_init(&ro->bo_map_lock, mtx_plain); |
| 88 |
|
| 89 |
if (strcmp(render_dev_name, "asahi") == 0) { |
| 90 |
#if defined(GALLIUM_ASAHI) |
| 91 |
ro->create_for_resource = renderonly_create_gpu_import_for_resource; |
| 92 |
screen = asahi_drm_screen_create_renderonly(ro->gpu_fd, ro, config); |
| 93 |
#endif |
| 94 |
} |
| 95 |
else if (strcmp(render_dev_name, "etnaviv") == 0) { |
| 96 |
#if defined(GALLIUM_ETNAVIV) |
| 97 |
ro->create_for_resource = renderonly_create_kms_dumb_buffer_for_resource; |
| 98 |
screen = etna_drm_screen_create_renderonly(ro->gpu_fd, ro, config); |
| 99 |
#endif |
| 100 |
} else if (strcmp(render_dev_name, "msm") == 0) { |
| 101 |
#if defined(GALLIUM_FREEDRENO) |
| 102 |
ro->create_for_resource = renderonly_create_kms_dumb_buffer_for_resource; |
| 103 |
screen = fd_drm_screen_create_renderonly(ro->gpu_fd, ro, config); |
| 104 |
#endif |
| 105 |
} else if (strcmp(render_dev_name, "lima") == 0) { |
| 106 |
#if defined(GALLIUM_LIMA) |
| 107 |
ro->create_for_resource = renderonly_create_kms_dumb_buffer_for_resource; |
| 108 |
screen = lima_drm_screen_create_renderonly(ro->gpu_fd, ro, config); |
| 109 |
#endif |
| 110 |
} else if (strcmp(render_dev_name, "panfrost") == 0 || |
| 111 |
strcmp(render_dev_name, "panthor") == 0) { |
| 112 |
#if defined(GALLIUM_PANFROST) |
| 113 |
ro->create_for_resource = panfrost_create_kms_dumb_buffer_for_resource; |
| 114 |
screen = panfrost_drm_screen_create_renderonly(ro->gpu_fd, ro, config); |
| 115 |
#endif |
| 116 |
} else if (strcmp(render_dev_name, "v3d") == 0) { |
| 117 |
#if defined(GALLIUM_V3D) |
| 118 |
ro->create_for_resource = renderonly_create_kms_dumb_buffer_for_resource; |
| 119 |
screen = v3d_drm_screen_create_renderonly(ro->gpu_fd, ro, config); |
| 120 |
#endif |
| 121 |
} else if (strcmp(render_dev_name, "vc4") == 0) { |
| 122 |
#if defined(GALLIUM_VC4) |
| 123 |
/* Passes the vc4-allocated BO through to the KMS-only DRM device using |
| 124 |
* PRIME buffer sharing. The VC4 BO must be linear, which the SCANOUT |
| 125 |
* flag on allocation will have ensured. |
| 126 |
*/ |
| 127 |
ro->create_for_resource = renderonly_create_gpu_import_for_resource; |
| 128 |
screen = vc4_drm_screen_create_renderonly(ro->gpu_fd, ro, config); |
| 129 |
#endif |
| 130 |
} |
| 131 |
|
| 132 |
free(render_dev_name); |
| 133 |
|
| 134 |
#if defined(GALLIUM_ZINK) |
| 135 |
if (!screen) { |
| 136 |
ro->create_for_resource = renderonly_create_kms_dumb_buffer_for_resource; |
| 137 |
screen = zink_drm_create_screen_renderonly(ro->gpu_fd, ro, config); |
| 138 |
} |
| 139 |
#endif |
| 140 |
|
| 141 |
/* test if the screen is actually graphics render capable */ |
| 142 |
if (screen) { |
| 143 |
if (screen->caps.graphics) |
| 144 |
goto out; |
| 145 |
else { |
| 146 |
screen->destroy(screen); |
| 147 |
screen = NULL; |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
out: |
| 153 |
for (unsigned int i = 0; i < n_devices; i++) |
| 154 |
close(gpu_fds[i]); |
| 155 |
free(gpu_fds); |
| 156 |
return screen; |
| 157 |
} |