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