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