Hi >> drivers/gpu/drm/tiny/appletbdrm.c | 702 ++++++++++++++++++++++++++++++ > > You also have to add the driver to MAINTAINERS. Sorry, forgot about that > >> 3 files changed, 716 insertions(+) >> create mode 100644 drivers/gpu/drm/tiny/appletbdrm.c >> >> diff --git a/drivers/gpu/drm/tiny/Kconfig b/drivers/gpu/drm/tiny/Kconfig >> index 94cbdb133..76d1a2f9f 100644 >> --- a/drivers/gpu/drm/tiny/Kconfig >> +++ b/drivers/gpu/drm/tiny/Kconfig >> @@ -1,5 +1,18 @@ >> # SPDX-License-Identifier: GPL-2.0-only >> +config DRM_APPLETBDRM >> + tristate "DRM support for Apple Touch Bars" >> + depends on DRM && USB && MMU >> + select DRM_GEM_SHMEM_HELPER >> + select DRM_KMS_HELPER >> + select HID_APPLETB_BL >> + help >> + Say Y here if you want support for the display of Touch Bars on x86 >> + MacBook Pros. >> + >> + To compile this driver as a module, choose M here: the >> + module will be called appletbdrm. >> + >> config DRM_ARCPGU >> tristate "ARC PGU" >> depends on DRM && OF >> diff --git a/drivers/gpu/drm/tiny/Makefile b/drivers/gpu/drm/tiny/Makefile >> index 60816d2eb..0a3a7837a 100644 >> --- a/drivers/gpu/drm/tiny/Makefile >> +++ b/drivers/gpu/drm/tiny/Makefile >> @@ -1,5 +1,6 @@ >> # SPDX-License-Identifier: GPL-2.0-only >> +obj-$(CONFIG_DRM_APPLETBDRM) += appletbdrm.o >> obj-$(CONFIG_DRM_ARCPGU) += arcpgu.o >> obj-$(CONFIG_DRM_BOCHS) += bochs.o >> obj-$(CONFIG_DRM_CIRRUS_QEMU) += cirrus-qemu.o >> diff --git a/drivers/gpu/drm/tiny/appletbdrm.c b/drivers/gpu/drm/tiny/appletbdrm.c >> new file mode 100644 >> index 000000000..f056f6280 >> --- /dev/null >> +++ b/drivers/gpu/drm/tiny/appletbdrm.c >> @@ -0,0 +1,702 @@ >> +// SPDX-License-Identifier: GPL-2.0 >> +/* >> + * Apple Touch Bar DRM Driver >> + * >> + * Copyright (c) 2023 Kerem Karabay <kekrby@xxxxxxxxx> >> + */ >> + >> +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt >> + >> +#include <linux/module.h> >> +#include <linux/unaligned.h> >> +#include <linux/usb.h> >> + >> +#include <drm/drm_atomic.h> >> +#include <drm/drm_atomic_helper.h> >> +#include <drm/drm_crtc.h> >> +#include <drm/drm_damage_helper.h> >> +#include <drm/drm_drv.h> >> +#include <drm/drm_encoder.h> >> +#include <drm/drm_format_helper.h> >> +#include <drm/drm_fourcc.h> >> +#include <drm/drm_gem_atomic_helper.h> >> +#include <drm/drm_gem_framebuffer_helper.h> >> +#include <drm/drm_gem_shmem_helper.h> >> +#include <drm/drm_plane.h> >> +#include <drm/drm_probe_helper.h> >> + >> +#define __APPLETBDRM_MSG_STR4(str4) ((__le32 __force)((str4[0] << 24) | (str4[1] << 16) | (str4[2] << 8) | str4[3])) >> +#define __APPLETBDRM_MSG_TOK4(tok4) __APPLETBDRM_MSG_STR4(#tok4) >> + >> +#define APPLETBDRM_PIXEL_FORMAT __APPLETBDRM_MSG_TOK4(RGBA) /* The actual format is BGR888 */ >> +#define APPLETBDRM_BITS_PER_PIXEL 24 >> + >> +#define APPLETBDRM_MSG_CLEAR_DISPLAY __APPLETBDRM_MSG_TOK4(CLRD) >> +#define APPLETBDRM_MSG_GET_INFORMATION __APPLETBDRM_MSG_TOK4(GINF) >> +#define APPLETBDRM_MSG_UPDATE_COMPLETE __APPLETBDRM_MSG_TOK4(UDCL) >> +#define APPLETBDRM_MSG_SIGNAL_READINESS __APPLETBDRM_MSG_TOK4(REDY) >> + >> +#define APPLETBDRM_BULK_MSG_TIMEOUT 1000 >> + >> +#define drm_to_adev(_drm) container_of(_drm, struct appletbdrm_device, drm) >> +#define adev_to_udev(adev) interface_to_usbdev(to_usb_interface(adev->dev)) >> + >> +struct appletbdrm_msg_request_header { >> + __le16 unk_00; >> + __le16 unk_02; >> + __le32 unk_04; >> + __le32 unk_08; >> + __le32 size; >> +} __packed; >> + >> +struct appletbdrm_msg_response_header { >> + u8 unk_00[16]; >> + __le32 msg; >> +} __packed; >> + >> +struct appletbdrm_msg_simple_request { >> + struct appletbdrm_msg_request_header header; >> + __le32 msg; >> + u8 unk_14[8]; >> + __le32 size; >> +} __packed; >> + >> +struct appletbdrm_msg_information { >> + struct appletbdrm_msg_response_header header; >> + u8 unk_14[12]; >> + __le32 width; >> + __le32 height; >> + u8 bits_per_pixel; >> + __le32 bytes_per_row; >> + __le32 orientation; >> + __le32 bitmap_info; >> + __le32 pixel_format; >> + __le32 width_inches; /* floating point */ >> + __le32 height_inches; /* floating point */ >> +} __packed; >> + >> +struct appletbdrm_frame { >> + __le16 begin_x; >> + __le16 begin_y; >> + __le16 width; >> + __le16 height; >> + __le32 buf_size; >> + u8 buf[]; >> +} __packed; >> + >> +struct appletbdrm_fb_request_footer { >> + u8 unk_00[12]; >> + __le32 unk_0c; >> + u8 unk_10[12]; >> + __le32 unk_1c; >> + __le64 timestamp; >> + u8 unk_28[12]; >> + __le32 unk_34; >> + u8 unk_38[20]; >> + __le32 unk_4c; >> +} __packed; >> + >> +struct appletbdrm_fb_request { >> + struct appletbdrm_msg_request_header header; >> + __le16 unk_10; >> + u8 msg_id; >> + u8 unk_13[29]; >> + /* >> + * Contents of `data`: >> + * - struct appletbdrm_frame frames[]; >> + * - struct appletbdrm_fb_request_footer footer; >> + * - padding to make the total size a multiple of 16 >> + */ >> + u8 data[]; >> +} __packed; >> + >> +struct appletbdrm_fb_request_response { >> + struct appletbdrm_msg_response_header header; >> + u8 unk_14[12]; >> + __le64 timestamp; >> +} __packed; >> + >> +struct appletbdrm_device { >> + struct device *dev; >> + >> + unsigned int in_ep; >> + unsigned int out_ep; >> + >> + unsigned int width; >> + unsigned int height; >> + >> + struct drm_device drm; >> + struct drm_display_mode mode; >> + struct drm_connector connector; >> + struct drm_plane primary_plane; >> + struct drm_crtc crtc; >> + struct drm_encoder encoder; >> +}; >> + >> +static int appletbdrm_send_request(struct appletbdrm_device *adev, >> + struct appletbdrm_msg_request_header *request, size_t size) >> +{ >> + struct usb_device *udev = adev_to_udev(adev); >> + struct drm_device *drm = &adev->drm; >> + int ret, actual_size; >> + >> + ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, adev->out_ep), >> + request, size, &actual_size, APPLETBDRM_BULK_MSG_TIMEOUT); >> + if (ret) { >> + drm_err(drm, "Failed to send message (%d)\n", ret); >> + return ret; >> + } >> + >> + if (actual_size != size) { >> + drm_err(drm, "Actual size (%d) doesn't match expected size (%lu)\n", >> + actual_size, size); >> + return -EIO; >> + } >> + >> + return ret; >> +} >> + >> +static int appletbdrm_read_response(struct appletbdrm_device *adev, >> + struct appletbdrm_msg_response_header *response, >> + size_t size, u32 expected_response) >> +{ >> + struct usb_device *udev = adev_to_udev(adev); >> + struct drm_device *drm = &adev->drm; >> + int ret, actual_size; >> + bool readiness_signal_received = false; >> + >> +retry: >> + ret = usb_bulk_msg(udev, usb_rcvbulkpipe(udev, adev->in_ep), >> + response, size, &actual_size, APPLETBDRM_BULK_MSG_TIMEOUT); >> + if (ret) { >> + drm_err(drm, "Failed to read response (%d)\n", ret); >> + return ret; >> + } >> + >> + /* >> + * The device responds to the first request sent in a particular >> + * timeframe after the USB device configuration is set with a readiness >> + * signal, in which case the response should be read again >> + */ >> + if (response->msg == APPLETBDRM_MSG_SIGNAL_READINESS) { >> + if (!readiness_signal_received) { >> + readiness_signal_received = true; >> + goto retry; >> + } >> + >> + drm_err(drm, "Encountered unexpected readiness signal\n"); >> + return -EIO; >> + } >> + >> + if (actual_size != size) { >> + drm_err(drm, "Actual size (%d) doesn't match expected size (%lu)\n", >> + actual_size, size); >> + return -EIO; >> + } >> + >> + if (response->msg != expected_response) { >> + drm_err(drm, "Unexpected response from device (expected %p4ch found %p4ch)\n", >> + &expected_response, &response->msg); >> + return -EIO; >> + } >> + >> + return 0; >> +} >> + >> +static int appletbdrm_send_msg(struct appletbdrm_device *adev, u32 msg) >> +{ >> + struct appletbdrm_msg_simple_request *request; >> + int ret; >> + >> + request = kzalloc(sizeof(*request), GFP_KERNEL); >> + if (!request) >> + return -ENOMEM; >> + >> + request->header.unk_00 = cpu_to_le16(2); >> + request->header.unk_02 = cpu_to_le16(0x1512); >> + request->header.size = cpu_to_le32(sizeof(*request) - sizeof(request->header)); >> + request->msg = msg; >> + request->size = request->header.size; >> + >> + ret = appletbdrm_send_request(adev, &request->header, sizeof(*request)); >> + >> + kfree(request); > > This is temporary data for the send operation and save to free here? > >> + >> + return ret; >> +} >> + >> +static int appletbdrm_clear_display(struct appletbdrm_device *adev) >> +{ >> + return appletbdrm_send_msg(adev, APPLETBDRM_MSG_CLEAR_DISPLAY); >> +} >> + >> +static int appletbdrm_signal_readiness(struct appletbdrm_device *adev) >> +{ >> + return appletbdrm_send_msg(adev, APPLETBDRM_MSG_SIGNAL_READINESS); >> +} >> + >> +static int appletbdrm_get_information(struct appletbdrm_device *adev) >> +{ >> + struct appletbdrm_msg_information *info; >> + struct drm_device *drm = &adev->drm; >> + u8 bits_per_pixel; >> + u32 pixel_format; >> + int ret; >> + >> + info = kzalloc(sizeof(*info), GFP_KERNEL); >> + if (!info) >> + return -ENOMEM; >> + >> + ret = appletbdrm_send_msg(adev, APPLETBDRM_MSG_GET_INFORMATION); >> + if (ret) >> + return ret; >> + >> + ret = appletbdrm_read_response(adev, &info->header, sizeof(*info), >> + APPLETBDRM_MSG_GET_INFORMATION); >> + if (ret) >> + goto free_info; >> + >> + bits_per_pixel = info->bits_per_pixel; >> + pixel_format = get_unaligned(&info->pixel_format); >> + >> + adev->width = get_unaligned_le32(&info->width); >> + adev->height = get_unaligned_le32(&info->height); >> + >> + if (bits_per_pixel != APPLETBDRM_BITS_PER_PIXEL) { >> + drm_err(drm, "Encountered unexpected bits per pixel value (%d)\n", bits_per_pixel); >> + ret = -EINVAL; >> + goto free_info; >> + } >> + >> + if (pixel_format != APPLETBDRM_PIXEL_FORMAT) { >> + drm_err(drm, "Encountered unknown pixel format (%p4ch)\n", &pixel_format); >> + ret = -EINVAL; >> + goto free_info; >> + } >> + >> +free_info: >> + kfree(info); >> + >> + return ret; >> +} >> + >> +static u32 rect_size(struct drm_rect *rect) >> +{ >> + return drm_rect_width(rect) * drm_rect_height(rect) * (APPLETBDRM_BITS_PER_PIXEL / 8); >> +} >> + >> +static int appletbdrm_flush_damage(struct appletbdrm_device *adev, >> + struct drm_plane_state *old_state, >> + struct drm_plane_state *state) >> +{ >> + struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(state); >> + struct appletbdrm_fb_request_response *response; >> + struct appletbdrm_fb_request_footer *footer; >> + struct drm_atomic_helper_damage_iter iter; >> + struct drm_framebuffer *fb = state->fb; >> + struct appletbdrm_fb_request *request; >> + struct drm_device *drm = &adev->drm; >> + struct appletbdrm_frame *frame; >> + u64 timestamp = ktime_get_ns(); >> + struct drm_rect damage; >> + size_t frames_size = 0; >> + size_t request_size; >> + int ret; >> + >> + drm_atomic_helper_damage_iter_init(&iter, old_state, state); >> + drm_atomic_for_each_plane_damage(&iter, &damage) { >> + frames_size += struct_size(frame, buf, rect_size(&damage)); >> + } >> + >> + if (!frames_size) >> + return 0; >> + >> + request_size = ALIGN(sizeof(*request) + frames_size + sizeof(*footer), 16); >> + >> + request = kzalloc(request_size, GFP_KERNEL); >> + if (!request) >> + return -ENOMEM; >> + >> + response = kzalloc(sizeof(*response), GFP_KERNEL); >> + if (!response) { >> + ret = -ENOMEM; >> + goto free_request; >> + } > > This code runs in the middle of the atomic update. It's then too late to do error handling. If these allocs fail, the display hardware will remain in undefined state. > > It is much preferable to allocate this memory in the atomic_check. To do so, you need a plane-state structure. Allocate the memory for request and response in the plane's atomic-check helper. If that fails, you can return an error there. Store the pointers and the request size in your plane-state structure and fetch them here. The memory should later be freed in the plane's destroy callback. For an example, see how the ssd130x driver treats a buffer for a similar use case at [1]. > > [1] https://elixir.bootlin.com/linux/v6.13.2/source/drivers/gpu/drm/solomon/ssd130x.c#L1136 I’ve tried these changes, seem to be breaking the driver: —>8—