1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright 2023, Intel Corporation. 4 */ 5 6 #include <drm/drm_print.h> 7 #include <drm/intel/i915_hdcp_interface.h> 8 #include <linux/delay.h> 9 10 #include "abi/gsc_command_header_abi.h" 11 #include "intel_hdcp_gsc.h" 12 #include "intel_hdcp_gsc_message.h" 13 #include "xe_bo.h" 14 #include "xe_device.h" 15 #include "xe_device_types.h" 16 #include "xe_force_wake.h" 17 #include "xe_gsc_proxy.h" 18 #include "xe_gsc_submit.h" 19 #include "xe_map.h" 20 #include "xe_pm.h" 21 #include "xe_uc_fw.h" 22 23 #define HECI_MEADDRESS_HDCP 18 24 25 struct intel_hdcp_gsc_message { 26 struct xe_bo *hdcp_bo; 27 u64 hdcp_cmd_in; 28 u64 hdcp_cmd_out; 29 }; 30 31 #define HDCP_GSC_HEADER_SIZE sizeof(struct intel_gsc_mtl_header) 32 33 bool intel_hdcp_gsc_cs_required(struct intel_display *display) 34 { 35 return DISPLAY_VER(display) >= 14; 36 } 37 38 bool intel_hdcp_gsc_check_status(struct intel_display *display) 39 { 40 struct xe_device *xe = to_xe_device(display->drm); 41 struct xe_tile *tile = xe_device_get_root_tile(xe); 42 struct xe_gt *gt = tile->media_gt; 43 struct xe_gsc *gsc = >->uc.gsc; 44 bool ret = true; 45 unsigned int fw_ref; 46 47 if (!gsc || !xe_uc_fw_is_enabled(&gsc->fw)) { 48 drm_dbg_kms(&xe->drm, 49 "GSC Components not ready for HDCP2.x\n"); 50 return false; 51 } 52 53 xe_pm_runtime_get(xe); 54 fw_ref = xe_force_wake_get(gt_to_fw(gt), XE_FW_GSC); 55 if (!fw_ref) { 56 drm_dbg_kms(&xe->drm, 57 "failed to get forcewake to check proxy status\n"); 58 ret = false; 59 goto out; 60 } 61 62 if (!xe_gsc_proxy_init_done(gsc)) 63 ret = false; 64 65 xe_force_wake_put(gt_to_fw(gt), fw_ref); 66 out: 67 xe_pm_runtime_put(xe); 68 return ret; 69 } 70 71 /*This function helps allocate memory for the command that we will send to gsc cs */ 72 static int intel_hdcp_gsc_initialize_message(struct intel_display *display, 73 struct intel_hdcp_gsc_message *hdcp_message) 74 { 75 struct xe_device *xe = to_xe_device(display->drm); 76 struct xe_bo *bo = NULL; 77 u64 cmd_in, cmd_out; 78 int ret = 0; 79 80 /* allocate object of two page for HDCP command memory and store it */ 81 bo = xe_bo_create_pin_map(xe, xe_device_get_root_tile(xe), NULL, PAGE_SIZE * 2, 82 ttm_bo_type_kernel, 83 XE_BO_FLAG_SYSTEM | 84 XE_BO_FLAG_GGTT); 85 86 if (IS_ERR(bo)) { 87 drm_err(display->drm, "Failed to allocate bo for HDCP streaming command!\n"); 88 ret = PTR_ERR(bo); 89 goto out; 90 } 91 92 cmd_in = xe_bo_ggtt_addr(bo); 93 cmd_out = cmd_in + PAGE_SIZE; 94 xe_map_memset(xe, &bo->vmap, 0, 0, bo->size); 95 96 hdcp_message->hdcp_bo = bo; 97 hdcp_message->hdcp_cmd_in = cmd_in; 98 hdcp_message->hdcp_cmd_out = cmd_out; 99 out: 100 return ret; 101 } 102 103 static int intel_hdcp_gsc_hdcp2_init(struct intel_display *display) 104 { 105 struct intel_hdcp_gsc_message *hdcp_message; 106 int ret; 107 108 hdcp_message = kzalloc(sizeof(*hdcp_message), GFP_KERNEL); 109 110 if (!hdcp_message) 111 return -ENOMEM; 112 113 /* 114 * NOTE: No need to lock the comp mutex here as it is already 115 * going to be taken before this function called 116 */ 117 ret = intel_hdcp_gsc_initialize_message(display, hdcp_message); 118 if (ret) { 119 drm_err(display->drm, "Could not initialize hdcp_message\n"); 120 kfree(hdcp_message); 121 return ret; 122 } 123 124 display->hdcp.hdcp_message = hdcp_message; 125 return ret; 126 } 127 128 static const struct i915_hdcp_ops gsc_hdcp_ops = { 129 .initiate_hdcp2_session = intel_hdcp_gsc_initiate_session, 130 .verify_receiver_cert_prepare_km = 131 intel_hdcp_gsc_verify_receiver_cert_prepare_km, 132 .verify_hprime = intel_hdcp_gsc_verify_hprime, 133 .store_pairing_info = intel_hdcp_gsc_store_pairing_info, 134 .initiate_locality_check = intel_hdcp_gsc_initiate_locality_check, 135 .verify_lprime = intel_hdcp_gsc_verify_lprime, 136 .get_session_key = intel_hdcp_gsc_get_session_key, 137 .repeater_check_flow_prepare_ack = 138 intel_hdcp_gsc_repeater_check_flow_prepare_ack, 139 .verify_mprime = intel_hdcp_gsc_verify_mprime, 140 .enable_hdcp_authentication = intel_hdcp_gsc_enable_authentication, 141 .close_hdcp_session = intel_hdcp_gsc_close_session, 142 }; 143 144 int intel_hdcp_gsc_init(struct intel_display *display) 145 { 146 struct i915_hdcp_arbiter *data; 147 int ret; 148 149 data = kzalloc(sizeof(*data), GFP_KERNEL); 150 if (!data) 151 return -ENOMEM; 152 153 mutex_lock(&display->hdcp.hdcp_mutex); 154 display->hdcp.arbiter = data; 155 display->hdcp.arbiter->hdcp_dev = display->drm->dev; 156 display->hdcp.arbiter->ops = &gsc_hdcp_ops; 157 ret = intel_hdcp_gsc_hdcp2_init(display); 158 if (ret) 159 kfree(data); 160 161 mutex_unlock(&display->hdcp.hdcp_mutex); 162 163 return ret; 164 } 165 166 void intel_hdcp_gsc_fini(struct intel_display *display) 167 { 168 struct intel_hdcp_gsc_message *hdcp_message = 169 display->hdcp.hdcp_message; 170 struct i915_hdcp_arbiter *arb = display->hdcp.arbiter; 171 172 if (hdcp_message) { 173 xe_bo_unpin_map_no_vm(hdcp_message->hdcp_bo); 174 kfree(hdcp_message); 175 display->hdcp.hdcp_message = NULL; 176 } 177 178 kfree(arb); 179 display->hdcp.arbiter = NULL; 180 } 181 182 static int xe_gsc_send_sync(struct xe_device *xe, 183 struct intel_hdcp_gsc_message *hdcp_message, 184 u32 msg_size_in, u32 msg_size_out, 185 u32 addr_out_off) 186 { 187 struct xe_gt *gt = hdcp_message->hdcp_bo->tile->media_gt; 188 struct iosys_map *map = &hdcp_message->hdcp_bo->vmap; 189 struct xe_gsc *gsc = >->uc.gsc; 190 int ret; 191 192 ret = xe_gsc_pkt_submit_kernel(gsc, hdcp_message->hdcp_cmd_in, msg_size_in, 193 hdcp_message->hdcp_cmd_out, msg_size_out); 194 if (ret) { 195 drm_err(&xe->drm, "failed to send gsc HDCP msg (%d)\n", ret); 196 return ret; 197 } 198 199 if (xe_gsc_check_and_update_pending(xe, map, 0, map, addr_out_off)) 200 return -EAGAIN; 201 202 ret = xe_gsc_read_out_header(xe, map, addr_out_off, 203 sizeof(struct hdcp_cmd_header), NULL); 204 205 return ret; 206 } 207 208 ssize_t intel_hdcp_gsc_msg_send(struct xe_device *xe, u8 *msg_in, 209 size_t msg_in_len, u8 *msg_out, 210 size_t msg_out_len) 211 { 212 const size_t max_msg_size = PAGE_SIZE - HDCP_GSC_HEADER_SIZE; 213 struct intel_hdcp_gsc_message *hdcp_message; 214 u64 host_session_id; 215 u32 msg_size_in, msg_size_out; 216 u32 addr_out_off, addr_in_wr_off = 0; 217 int ret, tries = 0; 218 219 if (msg_in_len > max_msg_size || msg_out_len > max_msg_size) { 220 ret = -ENOSPC; 221 goto out; 222 } 223 224 msg_size_in = msg_in_len + HDCP_GSC_HEADER_SIZE; 225 msg_size_out = msg_out_len + HDCP_GSC_HEADER_SIZE; 226 hdcp_message = xe->display.hdcp.hdcp_message; 227 addr_out_off = PAGE_SIZE; 228 229 host_session_id = xe_gsc_create_host_session_id(); 230 xe_pm_runtime_get_noresume(xe); 231 addr_in_wr_off = xe_gsc_emit_header(xe, &hdcp_message->hdcp_bo->vmap, 232 addr_in_wr_off, HECI_MEADDRESS_HDCP, 233 host_session_id, msg_in_len); 234 xe_map_memcpy_to(xe, &hdcp_message->hdcp_bo->vmap, addr_in_wr_off, 235 msg_in, msg_in_len); 236 /* 237 * Keep sending request in case the pending bit is set no need to add 238 * message handle as we are using same address hence loc. of header is 239 * same and it will contain the message handle. we will send the message 240 * 20 times each message 50 ms apart 241 */ 242 do { 243 ret = xe_gsc_send_sync(xe, hdcp_message, msg_size_in, msg_size_out, 244 addr_out_off); 245 246 /* Only try again if gsc says so */ 247 if (ret != -EAGAIN) 248 break; 249 250 msleep(50); 251 252 } while (++tries < 20); 253 254 if (ret) 255 goto out; 256 257 xe_map_memcpy_from(xe, msg_out, &hdcp_message->hdcp_bo->vmap, 258 addr_out_off + HDCP_GSC_HEADER_SIZE, 259 msg_out_len); 260 261 out: 262 xe_pm_runtime_put(xe); 263 return ret; 264 } 265