Hi In continuation to my previous mail. >> + >> +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? Probably yes. If I understand correctly, it’s needed to make the touchbar go into the display mode, from the hid keyboard mode. We here are doing the same as the Windows driver [1] for this does. [1] https://github.com/imbushuo/DFRDisplayKm/blob/master/src/DFRDisplayKm/include/Dfr.h#L3 > >> + >> + 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 > > >> + >> + ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE); >> + if (ret) { >> + drm_err(drm, "Failed to start CPU framebuffer access (%d)\n", ret); >> + goto free_response; >> + } >> + >> + request->header.unk_00 = cpu_to_le16(2); >> + request->header.unk_02 = cpu_to_le16(0x12); >> + request->header.unk_04 = cpu_to_le32(9); >> + request->header.size = cpu_to_le32(request_size - sizeof(request->header)); >> + request->unk_10 = cpu_to_le16(1); >> + request->msg_id = timestamp & 0xff; >> + >> + frame = (struct appletbdrm_frame *)request->data; >> + >> + drm_atomic_helper_damage_iter_init(&iter, old_state, state); >> + drm_atomic_for_each_plane_damage(&iter, &damage) { >> + struct iosys_map dst = IOSYS_MAP_INIT_VADDR(frame->buf); >> + u32 buf_size = rect_size(&damage); >> + >> + /* >> + * The coordinates need to be translated to the coordinate >> + * system the device expects, see the comment in >> + * appletbdrm_setup_mode_config >> + */ >> + frame->begin_x = cpu_to_le16(damage.y1); >> + frame->begin_y = cpu_to_le16(adev->height - damage.x2); >> + frame->width = cpu_to_le16(drm_rect_height(&damage)); >> + frame->height = cpu_to_le16(drm_rect_width(&damage)); >> + frame->buf_size = cpu_to_le32(buf_size); >> + >> + ret = drm_fb_blit(&dst, NULL, DRM_FORMAT_BGR888, >> + &shadow_plane_state->data[0], fb, &damage, &shadow_plane_state->fmtcnv_state); > > Can we void the use of drm_fb_blit()? Since you know all formats in advance, just do > > switch (format) > case XRGB8888: drm_fb_xrgb888_to_bgr888() break default: > drm_fb_memcpy() break }We use blit in simpledrm and ofdrm, where we don't know the formats and output buffers in advance. But it's really not so great in other drivers, I think. I think you mean this: #include <drm/drm_framebuffer.h> switch (fb->format->format) { case DRM_FORMAT_XRGB8888: drm_fb_xrgb8888_to_bgr888(&dst, NULL, &shadow_plane_state->data[0], fb, &damage, &shadow_plane_state->fmtcnv_state); break; default: drm_fb_memcpy(&dst, NULL, &shadow_plane_state->data[0], fb, &damage); break; } >> + if (ret) { >> + drm_err(drm, "Failed to copy damage clip (%d)\n", ret); >> + goto end_fb_cpu_access; >> + } >> + >> + frame = (void *)frame + struct_size(frame, buf, buf_size); >> + } >> + >> + footer = (struct appletbdrm_fb_request_footer *)&request->data[frames_size]; >> + >> + footer->unk_0c = cpu_to_le32(0xfffe); >> + footer->unk_1c = cpu_to_le32(0x80001); >> + footer->unk_34 = cpu_to_le32(0x80002); >> + footer->unk_4c = cpu_to_le32(0xffff); >> + footer->timestamp = cpu_to_le64(timestamp); >> + >> + ret = appletbdrm_send_request(adev, &request->header, request_size); >> + if (ret) >> + goto end_fb_cpu_access; >> + >> + ret = appletbdrm_read_response(adev, &response->header, sizeof(*response), >> + APPLETBDRM_MSG_UPDATE_COMPLETE); >> + if (ret) >> + goto end_fb_cpu_access; >> + >> + if (response->timestamp != footer->timestamp) { >> + drm_err(drm, "Response timestamp (%llu) doesn't match request timestamp (%llu)\n", >> + le64_to_cpu(response->timestamp), timestamp); >> + goto end_fb_cpu_access; >> + } >> + >> +end_fb_cpu_access: >> + drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE); >> +free_response: >> + kfree(response); >> +free_request: >> + kfree(request); >> + >> + return ret; >> +} >> + >> +static int appletbdrm_connector_helper_get_modes(struct drm_connector *connector) >> +{ >> + struct appletbdrm_device *adev = drm_to_adev(connector->dev); >> + >> + return drm_connector_helper_get_modes_fixed(connector, &adev->mode); >> +} >> + >> +static const u32 appletbdrm_primary_plane_formats[] = { >> + DRM_FORMAT_BGR888, >> + DRM_FORMAT_XRGB8888, /* emulated */ >> +}; >> + >> +static int appletbdrm_primary_plane_helper_atomic_check(struct drm_plane *plane, >> + struct drm_atomic_state *state) >> +{ >> + struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane); >> + struct drm_crtc *new_crtc = new_plane_state->crtc; >> + struct drm_crtc_state *new_crtc_state = NULL; >> + int ret; >> + >> + if (new_crtc) >> + new_crtc_state = drm_atomic_get_new_crtc_state(state, new_crtc); >> + >> + ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state, >> + DRM_PLANE_NO_SCALING, >> + DRM_PLANE_NO_SCALING, >> + false, false); >> + if (ret) >> + return ret; >> + else if (!new_plane_state->visible) >> + return 0; >> + >> + return 0; >> +} >> + >> +static void appletbdrm_primary_plane_helper_atomic_update(struct drm_plane *plane, >> + struct drm_atomic_state *old_state) >> +{ >> + struct appletbdrm_device *adev = drm_to_adev(plane->dev); >> + struct drm_device *drm = plane->dev; >> + struct drm_plane_state *plane_state = plane->state; >> + struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(old_state, plane); >> + int idx; > > plane_state->fb can be NULL if the plane's atomic_disable is not set. At a minimum you should check this first and return if so. > > A better idea is to implement atomic_disable. You can try to move the code from appletbdrm_crtc_helper_atomic_disable() to the plane's atomic disable and remove the CRTC helper. That would put all drawing code into the plane. The CRTC is more about programming a display mode. >> + >> + if (!drm_dev_enter(drm, &idx)) >> + return; >> + >> + appletbdrm_flush_damage(adev, old_plane_state, plane_state); >> + >> + drm_dev_exit(idx); >> +} >> + >> +static const struct drm_plane_helper_funcs appletbdrm_primary_plane_helper_funcs = { >> + DRM_GEM_SHADOW_PLANE_HELPER_FUNCS, >> + .atomic_check = appletbdrm_primary_plane_helper_atomic_check, >> + .atomic_update = appletbdrm_primary_plane_helper_atomic_update, >> +}; >> + >> +static const struct drm_plane_funcs appletbdrm_primary_plane_funcs = { >> + .update_plane = drm_atomic_helper_update_plane, >> + .disable_plane = drm_atomic_helper_disable_plane, >> + .destroy = drm_plane_cleanup, >> + DRM_GEM_SHADOW_PLANE_FUNCS, >> +}; >> + >> +static enum drm_mode_status appletbdrm_crtc_helper_mode_valid(struct drm_crtc *crtc, >> + const struct drm_display_mode *mode) >> +{ >> + struct appletbdrm_device *adev = drm_to_adev(crtc->dev); >> + >> + return drm_crtc_helper_mode_valid_fixed(crtc, mode, &adev->mode); >> +} >> + >> +static void appletbdrm_crtc_helper_atomic_disable(struct drm_crtc *crtc, >> + struct drm_atomic_state *crtc_state) >> +{ >> + struct appletbdrm_device *adev = drm_to_adev(crtc->dev); >> + int idx; >> + >> + if (!drm_dev_enter(&adev->drm, &idx)) >> + return; >> + >> + appletbdrm_clear_display(adev); >> + >> + drm_dev_exit(idx); >> +} >> + >> +static const struct drm_mode_config_funcs appletbdrm_mode_config_funcs = { >> + .fb_create = drm_gem_fb_create_with_dirty, >> + .atomic_check = drm_atomic_helper_check, >> + .atomic_commit = drm_atomic_helper_commit, >> +}; >> + >> +static const struct drm_connector_funcs appletbdrm_connector_funcs = { >> + .reset = drm_atomic_helper_connector_reset, >> + .destroy = drm_connector_cleanup, >> + .fill_modes = drm_helper_probe_single_connector_modes, >> + .atomic_destroy_state = drm_atomic_helper_connector_destroy_state, >> + .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state, >> +}; >> + >> +static const struct drm_connector_helper_funcs appletbdrm_connector_helper_funcs = { >> + .get_modes = appletbdrm_connector_helper_get_modes, >> +}; >> + >> +static const struct drm_crtc_helper_funcs appletbdrm_crtc_helper_funcs = { >> + .mode_valid = appletbdrm_crtc_helper_mode_valid, >> + .atomic_disable = appletbdrm_crtc_helper_atomic_disable, >> +}; >> + >> +static const struct drm_crtc_funcs appletbdrm_crtc_funcs = { >> + .reset = drm_atomic_helper_crtc_reset, >> + .destroy = drm_crtc_cleanup, >> + .set_config = drm_atomic_helper_set_config, >> + .page_flip = drm_atomic_helper_page_flip, >> + .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, >> + .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, >> +}; >> + >> +static const struct drm_encoder_funcs appletbdrm_encoder_funcs = { >> + .destroy = drm_encoder_cleanup, >> +}; >> + >> +DEFINE_DRM_GEM_FOPS(appletbdrm_drm_fops); >> + >> +static const struct drm_driver appletbdrm_drm_driver = { >> + DRM_GEM_SHMEM_DRIVER_OPS, > > For USB devices, we need special wiring to make PRIME work. The PRIME device must support DMA, but a USB device doesn't. So we pass the USB controller device instead. See [2] for what udl does and how it obtains dmadev. > > [2] https://elixir.bootlin.com/linux/v6.14-rc3/source/drivers/gpu/drm/udl/udl_drv.c#L76 Disregard my previous reply for this. I believe you meant by this?: —>8—