Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature to search for KMS DRI card #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

matviizorin
Copy link

@matviizorin matviizorin commented Jul 23, 2020

Most modern SOCs have separate IP cores for GPU and Display Unit (KMS). To work properly mesa requires initialize gbm interface using KMS card. Mesa uses it to allocate buffers with SCANOUT flag and does not require fd or pathname explicitly specified for GPU. Mesa will search for GPU and load the proper driver.

Also, there is no warranty that the KMS card will always have /dev/dri/card0 path and GPU - /dev/dri/card1. Order can depend on many factors. For example: on the rpi4 board, it was observed that enabling the WIFI kernel module swapping the card order. Therefore searching for the KMS card is the only efficient solution.

The has_kms_dev function returns true when the libdrm function is returned resources and the target device has at least one CTRC, connector, and encoder. The open_first_kms_dev function returns opened file descriptor on success using the previous function to check for each device. It also returns zeroed value for the case of KMS absence, or the -EINVAL on glob function error.

In the case of absence of the "hwc.drm.device" system property the first KMS device or the default path /dev/dri/renderD128 will be used to open.

A similar solution has been created and merged for drm-hwcomposer (MR#108).

@rsglobal
Copy link
Contributor

Thank you for this feature.
I have an issue with it:
When there is no access to /dev/dri/card0 device probably due to different user/group client (not system/graphics) then memory allocation fails.

@rsglobal
Copy link
Contributor

Here is my uevent rules:

/dev/dri/card0                                         0660    system       graphics
/dev/dri/card1                                         0660    system       graphics
/dev/dri/renderD128                                    0666    system       graphics

Most modern SOCs have separate IP cores for GPU and Display Unit (KMS).
To work properly mesa requires initialize gbm interface using KMS card.
Mesa uses it to allocate buffers with SCANOUT flag and does not require
fd or pathname explicitly specified for GPU. Mesa will search for GPU and
load the proper driver.

Also, there is no warranty that the KMS card will always have
/dev/dri/card0 path and GPU - /dev/dri/card1. Order can depend on many
factors. For example: on the rpi4 board, it was observed that enabling
the WIFI kernel module swapping the card order. Therefore searching for
the KMS card is the only efficient solution.

The is_kms_dev function returns true when the libdrm function is returned
resources and the target device has at least one CTRC, connector, and
encoder. The open_first_kms_dev function returns opened file descriptor
on success using the previous function to check for each device. It also
returns zeroed value for the case of KMS absence, or the -EINVAL on glob
function error.

In the case of absence of the "hwc.drm.device" system property the first
KMS device or the default path /dev/dri/renderD128 will be used to open.

Signed-off-by: Matvii Zorin <[email protected]>
Reviewed-by: Roman Stratiienko <[email protected]>
@matviizorin
Copy link
Author

@rsglobal, I've updated the PR. As for now, I handle the case if one of the paths is opened with an access error the search will be continued. Also, I extended the log prints. Could you review this solution?

rsglobal added a commit to GloDroid/glodroid_device that referenced this pull request Aug 21, 2020
Both drm_hwcomposer and gralloc can now detect KMS device.

Functionality was enabled by the commits:
https://gitlab.freedesktop.org/drm-hwcomposer/drm-hwcomposer/-/merge_requests/108
robherring/gbm_gralloc#16

Signed-off-by: Roman Stratiienko <[email protected]>
KonstaT referenced this pull request in lineage-rpi/android_external_gbm_gralloc Sep 23, 2020
Most modern SOCs have separate IP cores for GPU and Display Unit (KMS).
To work properly mesa requires initialize gbm interface using KMS card.
Mesa uses it to allocate buffers with SCANOUT flag and does not require
fd or pathname explicitly specified for GPU. Mesa will search for GPU and
load the proper driver.

Also, there is no warranty that the KMS card will always have
/dev/dri/card0 path and GPU - /dev/dri/card1. Order can depend on many
factors. For example: on the rpi4 board, it was observed that enabling
the WIFI kernel module swapping the card order. Therefore searching for
the KMS card is the only efficient solution.

The is_kms_dev function returns true when the libdrm function is returned
resources and the target device has at least one CTRC, connector, and
encoder. The open_first_kms_dev function returns opened file descriptor
on success using the previous function to check for each device. It also
returns zeroed value for the case of KMS absence, or the -EINVAL on glob
function error.

In the case of absence of the "hwc.drm.device" system property the first
KMS device or the default path /dev/dri/renderD128 will be used to open.

Signed-off-by: Matvii Zorin <[email protected]>
Reviewed-by: Roman Stratiienko <[email protected]>
@rsglobal
Copy link
Contributor

@matviizorin ,

Works fine for me. Thanks.

@rsglobal
Copy link
Contributor

@robherring ,

Could you merge this please?

if (fd < 0) {
ALOGE("failed to open %s with error %s", path, strerror(errno));
return NULL;
}
Copy link
Contributor

@rsglobal rsglobal Jan 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add drmDropMaster(fd); after this if () {} before line 337

See https://gitlab.freedesktop.org/drm-hwcomposer/drm-hwcomposer/-/merge_requests/134 for the explanation

@erfanoabdi
Copy link

This apparently breaks AMD gpus
(tested with drmDropMaster)

Comment on lines +321 to +325
if (path[path_len - 1] == '*') {
fd = open_first_kms_dev(path);
} else {
fd = open(path, O_RDWR | O_CLOEXEC);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems a bit cheeky to assume that every glob must end in * otherwise it will be treated as a regular path. It shouldn't be too bad to call open_first_kms_dev unconditionally?

break;

close(fd);
fd = 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Theoretically 0 is a valid fd (if it weren't usually used for stdin), this should probably be -1 (and the <= 0 above should be < 0).

{
glob_t glob_buf;
memset(&glob_buf, 0, sizeof(glob_buf));
int fd;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will glob() return 0 (instead of an error-code) if it found zero matching paths? If so, this should be initialized as to not return garbage when the loop never runs.

@rsglobal
Copy link
Contributor

This contribution was a part of GloDroid development activity. At this particular moment we almost switched to minigbm's gbm backend, where a little different logic is implemented.
In a few words KMS card node is opened only if rendernode with "lima,panfrost,etnaviv,freedreno,v3d,vc4" found. Otherwise it opens render node. (this logic used by mesa3d build system to enable kms entrypoints)

@matviizorin, Are there any plans to finish this? If not, consider closing it down so you don't mislead people.

@MarijnS95
Copy link

@rsglobal Thanks for the info - I assumed as much given the date and silence of this PR, but thought to comment this anyway in case anyone feels like reviewing/merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants