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