--- .../apple-touchbar-advanced-0.1/appletbdrm.c | 68 +++++++++++++------ 1 file changed, 46 insertions(+), 22 deletions(-) diff --git a/usr/src/apple-touchbar-advanced-0.1/appletbdrm.c b/usr/src/apple-touchbar-advanced-0.1/appletbdrm.c index f2d9113..cb13b36 100644 --- a/usr/src/apple-touchbar-advanced-0.1/appletbdrm.c +++ b/usr/src/apple-touchbar-advanced-0.1/appletbdrm.c @@ -133,6 +133,17 @@ struct appletbdrm_device { struct drm_encoder encoder; }; +struct appletbdrm_plane_state { + struct drm_shadow_plane_state base; + u8 *request_buffer; + u8 *response_buffer; +}; + +static inline struct appletbdrm_plane_state *to_appletbdrm_plane_state(struct drm_plane_state *state) +{ + return container_of(state, struct appletbdrm_plane_state, base.base); +} + static int appletbdrm_send_request(struct appletbdrm_device *adev, struct appletbdrm_msg_request_header *request, size_t size) { @@ -311,24 +322,6 @@ static int appletbdrm_flush_damage(struct appletbdrm_device *adev, 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; - } - - 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); @@ -389,10 +382,6 @@ static int appletbdrm_flush_damage(struct appletbdrm_device *adev, end_fb_cpu_access: drm_gem_fb_end_cpu_access(fb, DMA_FROM_DEVICE); -free_response: - kfree(response); -free_request: - kfree(request); return ret; } @@ -415,6 +404,15 @@ static int appletbdrm_primary_plane_helper_atomic_check(struct drm_plane *plane, 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; + struct appletbdrm_plane_state *appletbdrm_state = to_appletbdrm_plane_state(new_plane_state); + struct drm_device *drm = plane->dev; + struct drm_plane_state *plane_state = plane->state; + struct appletbdrm_fb_request_response *response; + struct appletbdrm_fb_request_footer *footer; + struct drm_framebuffer *fb = plane_state->fb; + struct appletbdrm_fb_request *request; + size_t frames_size = 0; + size_t request_size; int ret; if (new_crtc) @@ -429,6 +427,22 @@ static int appletbdrm_primary_plane_helper_atomic_check(struct drm_plane *plane, else if (!new_plane_state->visible) return 0; + request_size = ALIGN(sizeof(*request) + frames_size + sizeof(*footer), 16); + + appletbdrm_state->request_buffer = kzalloc(request_size, GFP_KERNEL); + if (!request) + return -ENOMEM; + + appletbdrm_state->response_buffer = kzalloc(sizeof(*response), GFP_KERNEL); + if (!response) { + ret = -ENOMEM; + } + + 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); + } + return 0; } @@ -464,6 +478,15 @@ static void appletbdrm_primary_plane_helper_atomic_disable(struct drm_plane *pla drm_dev_exit(idx); } +static void appletbdrm_primary_plane_destroy_state(struct drm_plane *plane, + struct drm_plane_state *state) +{ + struct appletbdrm_plane_state *appletbdrm_state = to_appletbdrm_plane_state(state); + + kfree(appletbdrm_state->request_buffer); + kfree(appletbdrm_state->response_buffer); +} + 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, @@ -474,6 +497,7 @@ static const struct drm_plane_helper_funcs appletbdrm_primary_plane_helper_funcs static const struct drm_plane_funcs appletbdrm_primary_plane_funcs = { .update_plane = drm_atomic_helper_update_plane, .disable_plane = drm_atomic_helper_disable_plane, + .atomic_destroy_state = appletbdrm_primary_plane_destroy_state, .destroy = drm_plane_cleanup, DRM_GEM_SHADOW_PLANE_FUNCS, }; -- 2.43.0 â??>8-- > > >> + >> + 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. >> + 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. Code will be moved to planeâ??s atomic disable in next version >> + >> + 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 Basically you want me to simply add .gem_prime_import = udl_driver_gem_prime_import, here right? > >> + .name = "appletbdrm", >> + .desc = "Apple Touch Bar DRM Driver", >> + .date = "20230910", > > The date field has been removed recently. Will be removed > >> + .major = 1, >> + .minor = 0, >> + .driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_ATOMIC, >> + .fops = &appletbdrm_drm_fops, >> +}; >> + >> +static int appletbdrm_setup_mode_config(struct appletbdrm_device *adev) >> +{ >> + struct drm_connector *connector = &adev->connector; >> + struct drm_plane *primary_plane; >> + struct drm_crtc *crtc; >> + struct drm_encoder *encoder; >> + struct drm_device *drm = &adev->drm; >> + struct device *dev = adev->dev; >> + int ret; >> + >> + ret = drmm_mode_config_init(drm); >> + if (ret) >> + return dev_err_probe(dev, ret, "Failed to initialize mode configuration\n"); >> + >> + primary_plane = &adev->primary_plane; >> + ret = drm_universal_plane_init(drm, primary_plane, 0, >> + &appletbdrm_primary_plane_funcs, >> + appletbdrm_primary_plane_formats, >> + ARRAY_SIZE(appletbdrm_primary_plane_formats), >> + NULL, >> + DRM_PLANE_TYPE_PRIMARY, NULL); >> + if (ret) >> + return dev_err_probe(dev, ret, "Failed to initialize universal plane object\n"); >> + drm_plane_helper_add(primary_plane, &appletbdrm_primary_plane_helper_funcs); >> + drm_plane_enable_fb_damage_clips(primary_plane); >> + >> + crtc = &adev->crtc; >> + ret = drm_crtc_init_with_planes(drm, crtc, primary_plane, NULL, >> + &appletbdrm_crtc_funcs, NULL); >> + if (ret) >> + return dev_err_probe(dev, ret, "Failed to initialize CRTC object\n"); >> + drm_crtc_helper_add(crtc, &appletbdrm_crtc_helper_funcs); >> + >> + encoder = &adev->encoder; >> + ret = drm_encoder_init(drm, encoder, &appletbdrm_encoder_funcs, >> + DRM_MODE_ENCODER_DAC, NULL); >> + if (ret) >> + return dev_err_probe(dev, ret, "Failed to initialize encoder\n"); >> + encoder->possible_crtcs = drm_crtc_mask(crtc); >> + >> + /* >> + * The coordinate system used by the device is different from the >> + * coordinate system of the framebuffer in that the x and y axes are >> + * swapped, and that the y axis is inverted; so what the device reports >> + * as the height is actually the width of the framebuffer and vice >> + * versa >> + */ >> + drm->mode_config.min_width = 0; >> + drm->mode_config.min_height = 0; > >> + drm->mode_config.max_width = max(adev->height, DRM_SHADOW_PLANE_MAX_WIDTH); >> + drm->mode_config.max_height = max(adev->width, DRM_SHADOW_PLANE_MAX_HEIGHT); > > This might not work correctly. To use those SHADOW_PLANE_MAX constants, you have to fixup the damage coordinates to account for changes to the plane coordinates. Simpledrm does that by intersecting damage with dst_clip. [3] You also have to put the result at the right place within the display. > > [3] https://elixir.bootlin.com/linux/v6.14-rc3/source/drivers/gpu/drm/tiny/simpledrm.c#L646 > >> + drm->mode_config.preferred_depth = APPLETBDRM_BITS_PER_PIXEL; >> + drm->mode_config.funcs = &appletbdrm_mode_config_funcs; >> + >> + adev->mode = (struct drm_display_mode) { >> + DRM_MODE_INIT(60, adev->height, adev->width, >> + DRM_MODE_RES_MM(adev->height, 218), >> + DRM_MODE_RES_MM(adev->width, 218)) >> + }; >> + >> + ret = drm_connector_init(drm, connector, >> + &appletbdrm_connector_funcs, DRM_MODE_CONNECTOR_USB); >> + if (ret) >> + return dev_err_probe(dev, ret, "Failed to initialize connector\n"); >> + >> + drm_connector_helper_add(connector, &appletbdrm_connector_helper_funcs); >> + >> + ret = drm_connector_set_panel_orientation(connector, >> + DRM_MODE_PANEL_ORIENTATION_RIGHT_UP); >> + if (ret) >> + return dev_err_probe(dev, ret, "Failed to set panel orientation\n"); >> + >> + connector->display_info.non_desktop = true; >> + ret = drm_object_property_set_value(&connector->base, >> + drm->mode_config.non_desktop_property, true); >> + if (ret) >> + return dev_err_probe(dev, ret, "Failed to set non-desktop property\n"); >> + >> + ret = drm_connector_attach_encoder(connector, encoder); >> + >> + if (ret) >> + return dev_err_probe(dev, ret, "Failed to initialize simple display pipe\n"); >> + >> + drm_mode_config_reset(drm); >> + > > >> + ret = drm_dev_register(drm, 0); >> + if (ret) >> + return dev_err_probe(dev, ret, "Failed to register DRM device\n"); > > This call does not belong to the mode-setting pipeline and belongs into appletbdrm_probe(). > >> + >> + return 0; >> +} >> + >> +static int appletbdrm_probe(struct usb_interface *intf, >> + const struct usb_device_id *id) >> +{ >> + struct usb_endpoint_descriptor *bulk_in, *bulk_out; >> + struct device *dev = &intf->dev; >> + struct appletbdrm_device *adev; >> + int ret; >> + >> + ret = usb_find_common_endpoints(intf->cur_altsetting, &bulk_in, &bulk_out, NULL, NULL); >> + if (ret) >> + return dev_err_probe(dev, ret, "Failed to find bulk endpoints\n"); >> + >> + adev = devm_drm_dev_alloc(dev, &appletbdrm_drm_driver, struct appletbdrm_device, drm); >> + if (IS_ERR(adev)) >> + return PTR_ERR(adev); >> + >> + adev->dev = dev; >> + adev->in_ep = bulk_in->bEndpointAddress; >> + adev->out_ep = bulk_out->bEndpointAddress; >> + >> + usb_set_intfdata(intf, adev); > > Rather set the DRM device here and fetch it in the callbacks below. At some point, we might be able to share some of those helpers among drivers. > >> + >> + ret = appletbdrm_get_information(adev); >> + if (ret) >> + return dev_err_probe(dev, ret, "Failed to get display information\n"); >> + >> + ret = appletbdrm_signal_readiness(adev); >> + if (ret) >> + return dev_err_probe(dev, ret, "Failed to signal readiness\n"); >> + > >> + ret = appletbdrm_clear_display(adev); >> + if (ret) >> + return dev_err_probe(dev, ret, "Failed to clear display\n"); > > Clearing the display is not something usually done in probe. But I guess there's no better place. I'd do this as the final call in probe; after registering the device. That way, it acts a bit like an in-kernel DRM client. > > Best regards > Thomas > >> + >> + return appletbdrm_setup_mode_config(adev); >> +} >> + >> +static void appletbdrm_disconnect(struct usb_interface *intf) >> +{ >> + struct appletbdrm_device *adev = usb_get_intfdata(intf); >> + struct drm_device *drm = &adev->drm; >> + >> + drm_dev_unplug(drm); >> + drm_atomic_helper_shutdown(drm); >> +} >> + >> +static void appletbdrm_shutdown(struct usb_interface *intf) >> +{ >> + struct appletbdrm_device *adev = usb_get_intfdata(intf); >> + >> + /* >> + * The framebuffer needs to be cleared on shutdown since its content >> + * persists across boots >> + */ >> + drm_atomic_helper_shutdown(&adev->drm); >> +} >> + >> +static const struct usb_device_id appletbdrm_usb_id_table[] = { >> + { USB_DEVICE_INTERFACE_CLASS(0x05ac, 0x8302, USB_CLASS_AUDIO_VIDEO) }, >> + {} >> +}; >> +MODULE_DEVICE_TABLE(usb, appletbdrm_usb_id_table); >> + >> +static struct usb_driver appletbdrm_usb_driver = { >> + .name = "appletbdrm", >> + .probe = appletbdrm_probe, >> + .disconnect = appletbdrm_disconnect, >> + .shutdown = appletbdrm_shutdown, >> + .id_table = appletbdrm_usb_id_table, >> +}; >> +module_usb_driver(appletbdrm_usb_driver); >> + >> +MODULE_AUTHOR("Kerem Karabay <kekrby@xxxxxxxxx>"); >> +MODULE_DESCRIPTION("Apple Touch Bar DRM Driver"); >> +MODULE_LICENSE("GPL"); > > -- > -- > Thomas Zimmermann > Graphics Driver Developer > SUSE Software Solutions Germany GmbH > Frankenstrasse 146, 90461 Nuernberg, Germany > GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman > HRB 36809 (AG Nuernberg)