drm/hdcp: gathering hdcp related code into drm_hdcp.c
[sfrench/cifs-2.6.git] / drivers / gpu / drm / drm_hdcp.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2019 Intel Corporation.
4  *
5  * Authors:
6  * Ramalingam C <ramalingam.c@intel.com>
7  */
8
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/gfp.h>
12 #include <linux/export.h>
13 #include <linux/slab.h>
14 #include <linux/firmware.h>
15
16 #include <drm/drm_hdcp.h>
17 #include <drm/drm_sysfs.h>
18 #include <drm/drm_print.h>
19 #include <drm/drm_device.h>
20 #include <drm/drm_property.h>
21 #include <drm/drm_mode_object.h>
22 #include <drm/drm_connector.h>
23
24 struct hdcp_srm {
25         u32 revoked_ksv_cnt;
26         u8 *revoked_ksv_list;
27
28         /* Mutex to protect above struct member */
29         struct mutex mutex;
30 } *srm_data;
31
32 static inline void drm_hdcp_print_ksv(const u8 *ksv)
33 {
34         DRM_DEBUG("\t%#02x, %#02x, %#02x, %#02x, %#02x\n",
35                   ksv[0], ksv[1], ksv[2], ksv[3], ksv[4]);
36 }
37
38 static u32 drm_hdcp_get_revoked_ksv_count(const u8 *buf, u32 vrls_length)
39 {
40         u32 parsed_bytes = 0, ksv_count = 0, vrl_ksv_cnt, vrl_sz;
41
42         while (parsed_bytes < vrls_length) {
43                 vrl_ksv_cnt = *buf;
44                 ksv_count += vrl_ksv_cnt;
45
46                 vrl_sz = (vrl_ksv_cnt * DRM_HDCP_KSV_LEN) + 1;
47                 buf += vrl_sz;
48                 parsed_bytes += vrl_sz;
49         }
50
51         /*
52          * When vrls are not valid, ksvs are not considered.
53          * Hence SRM will be discarded.
54          */
55         if (parsed_bytes != vrls_length)
56                 ksv_count = 0;
57
58         return ksv_count;
59 }
60
61 static u32 drm_hdcp_get_revoked_ksvs(const u8 *buf, u8 *revoked_ksv_list,
62                                      u32 vrls_length)
63 {
64         u32 parsed_bytes = 0, ksv_count = 0;
65         u32 vrl_ksv_cnt, vrl_ksv_sz, vrl_idx = 0;
66
67         do {
68                 vrl_ksv_cnt = *buf;
69                 vrl_ksv_sz = vrl_ksv_cnt * DRM_HDCP_KSV_LEN;
70
71                 buf++;
72
73                 DRM_DEBUG("vrl: %d, Revoked KSVs: %d\n", vrl_idx++,
74                           vrl_ksv_cnt);
75                 memcpy(revoked_ksv_list, buf, vrl_ksv_sz);
76
77                 ksv_count += vrl_ksv_cnt;
78                 revoked_ksv_list += vrl_ksv_sz;
79                 buf += vrl_ksv_sz;
80
81                 parsed_bytes += (vrl_ksv_sz + 1);
82         } while (parsed_bytes < vrls_length);
83
84         return ksv_count;
85 }
86
87 static inline u32 get_vrl_length(const u8 *buf)
88 {
89         return drm_hdcp_be24_to_cpu(buf);
90 }
91
92 static int drm_hdcp_parse_hdcp1_srm(const u8 *buf, size_t count)
93 {
94         struct hdcp_srm_header *header;
95         u32 vrl_length, ksv_count;
96
97         if (count < (sizeof(struct hdcp_srm_header) +
98             DRM_HDCP_1_4_VRL_LENGTH_SIZE + DRM_HDCP_1_4_DCP_SIG_SIZE)) {
99                 DRM_ERROR("Invalid blob length\n");
100                 return -EINVAL;
101         }
102
103         header = (struct hdcp_srm_header *)buf;
104         DRM_DEBUG("SRM ID: 0x%x, SRM Ver: 0x%x, SRM Gen No: 0x%x\n",
105                   header->srm_id,
106                   be16_to_cpu(header->srm_version), header->srm_gen_no);
107
108         WARN_ON(header->reserved);
109
110         buf = buf + sizeof(*header);
111         vrl_length = get_vrl_length(buf);
112         if (count < (sizeof(struct hdcp_srm_header) + vrl_length) ||
113             vrl_length < (DRM_HDCP_1_4_VRL_LENGTH_SIZE +
114                           DRM_HDCP_1_4_DCP_SIG_SIZE)) {
115                 DRM_ERROR("Invalid blob length or vrl length\n");
116                 return -EINVAL;
117         }
118
119         /* Length of the all vrls combined */
120         vrl_length -= (DRM_HDCP_1_4_VRL_LENGTH_SIZE +
121                        DRM_HDCP_1_4_DCP_SIG_SIZE);
122
123         if (!vrl_length) {
124                 DRM_ERROR("No vrl found\n");
125                 return -EINVAL;
126         }
127
128         buf += DRM_HDCP_1_4_VRL_LENGTH_SIZE;
129         ksv_count = drm_hdcp_get_revoked_ksv_count(buf, vrl_length);
130         if (!ksv_count) {
131                 DRM_DEBUG("Revoked KSV count is 0\n");
132                 return count;
133         }
134
135         kfree(srm_data->revoked_ksv_list);
136         srm_data->revoked_ksv_list = kcalloc(ksv_count, DRM_HDCP_KSV_LEN,
137                                              GFP_KERNEL);
138         if (!srm_data->revoked_ksv_list) {
139                 DRM_ERROR("Out of Memory\n");
140                 return -ENOMEM;
141         }
142
143         if (drm_hdcp_get_revoked_ksvs(buf, srm_data->revoked_ksv_list,
144                                       vrl_length) != ksv_count) {
145                 srm_data->revoked_ksv_cnt = 0;
146                 kfree(srm_data->revoked_ksv_list);
147                 return -EINVAL;
148         }
149
150         srm_data->revoked_ksv_cnt = ksv_count;
151         return count;
152 }
153
154 static int drm_hdcp_parse_hdcp2_srm(const u8 *buf, size_t count)
155 {
156         struct hdcp_srm_header *header;
157         u32 vrl_length, ksv_count, ksv_sz;
158
159         if (count < (sizeof(struct hdcp_srm_header) +
160             DRM_HDCP_2_VRL_LENGTH_SIZE + DRM_HDCP_2_DCP_SIG_SIZE)) {
161                 DRM_ERROR("Invalid blob length\n");
162                 return -EINVAL;
163         }
164
165         header = (struct hdcp_srm_header *)buf;
166         DRM_DEBUG("SRM ID: 0x%x, SRM Ver: 0x%x, SRM Gen No: 0x%x\n",
167                   header->srm_id & DRM_HDCP_SRM_ID_MASK,
168                   be16_to_cpu(header->srm_version), header->srm_gen_no);
169
170         if (header->reserved)
171                 return -EINVAL;
172
173         buf = buf + sizeof(*header);
174         vrl_length = get_vrl_length(buf);
175
176         if (count < (sizeof(struct hdcp_srm_header) + vrl_length) ||
177             vrl_length < (DRM_HDCP_2_VRL_LENGTH_SIZE +
178             DRM_HDCP_2_DCP_SIG_SIZE)) {
179                 DRM_ERROR("Invalid blob length or vrl length\n");
180                 return -EINVAL;
181         }
182
183         /* Length of the all vrls combined */
184         vrl_length -= (DRM_HDCP_2_VRL_LENGTH_SIZE +
185                        DRM_HDCP_2_DCP_SIG_SIZE);
186
187         if (!vrl_length) {
188                 DRM_ERROR("No vrl found\n");
189                 return -EINVAL;
190         }
191
192         buf += DRM_HDCP_2_VRL_LENGTH_SIZE;
193         ksv_count = (*buf << 2) | DRM_HDCP_2_KSV_COUNT_2_LSBITS(*(buf + 1));
194         if (!ksv_count) {
195                 DRM_DEBUG("Revoked KSV count is 0\n");
196                 return count;
197         }
198
199         kfree(srm_data->revoked_ksv_list);
200         srm_data->revoked_ksv_list = kcalloc(ksv_count, DRM_HDCP_KSV_LEN,
201                                              GFP_KERNEL);
202         if (!srm_data->revoked_ksv_list) {
203                 DRM_ERROR("Out of Memory\n");
204                 return -ENOMEM;
205         }
206
207         ksv_sz = ksv_count * DRM_HDCP_KSV_LEN;
208         buf += DRM_HDCP_2_NO_OF_DEV_PLUS_RESERVED_SZ;
209
210         DRM_DEBUG("Revoked KSVs: %d\n", ksv_count);
211         memcpy(srm_data->revoked_ksv_list, buf, ksv_sz);
212
213         srm_data->revoked_ksv_cnt = ksv_count;
214         return count;
215 }
216
217 static inline bool is_srm_version_hdcp1(const u8 *buf)
218 {
219         return *buf == (u8)(DRM_HDCP_1_4_SRM_ID << 4);
220 }
221
222 static inline bool is_srm_version_hdcp2(const u8 *buf)
223 {
224         return *buf == (u8)(DRM_HDCP_2_SRM_ID << 4 | DRM_HDCP_2_INDICATOR);
225 }
226
227 static void drm_hdcp_srm_update(const u8 *buf, size_t count)
228 {
229         if (count < sizeof(struct hdcp_srm_header))
230                 return;
231
232         if (is_srm_version_hdcp1(buf))
233                 drm_hdcp_parse_hdcp1_srm(buf, count);
234         else if (is_srm_version_hdcp2(buf))
235                 drm_hdcp_parse_hdcp2_srm(buf, count);
236 }
237
238 void drm_hdcp_request_srm(struct drm_device *drm_dev)
239 {
240         char fw_name[36] = "display_hdcp_srm.bin";
241         const struct firmware *fw;
242
243         int ret;
244
245         ret = request_firmware_direct(&fw, (const char *)fw_name,
246                                       drm_dev->dev);
247         if (ret < 0)
248                 goto exit;
249
250         if (fw->size && fw->data)
251                 drm_hdcp_srm_update(fw->data, fw->size);
252
253 exit:
254         release_firmware(fw);
255 }
256
257 /**
258  * drm_hdcp_check_ksvs_revoked - Check the revoked status of the IDs
259  *
260  * @drm_dev: drm_device for which HDCP revocation check is requested
261  * @ksvs: List of KSVs (HDCP receiver IDs)
262  * @ksv_count: KSV count passed in through @ksvs
263  *
264  * This function reads the HDCP System renewability Message(SRM Table)
265  * from userspace as a firmware and parses it for the revoked HDCP
266  * KSVs(Receiver IDs) detected by DCP LLC. Once the revoked KSVs are known,
267  * revoked state of the KSVs in the list passed in by display drivers are
268  * decided and response is sent.
269  *
270  * SRM should be presented in the name of "display_hdcp_srm.bin".
271  *
272  * Returns:
273  * TRUE on any of the KSV is revoked, else FALSE.
274  */
275 bool drm_hdcp_check_ksvs_revoked(struct drm_device *drm_dev, u8 *ksvs,
276                                  u32 ksv_count)
277 {
278         u32 rev_ksv_cnt, cnt, i, j;
279         u8 *rev_ksv_list;
280
281         if (!srm_data)
282                 return false;
283
284         mutex_lock(&srm_data->mutex);
285         drm_hdcp_request_srm(drm_dev);
286
287         rev_ksv_cnt = srm_data->revoked_ksv_cnt;
288         rev_ksv_list = srm_data->revoked_ksv_list;
289
290         /* If the Revoked ksv list is empty */
291         if (!rev_ksv_cnt || !rev_ksv_list) {
292                 mutex_unlock(&srm_data->mutex);
293                 return false;
294         }
295
296         for  (cnt = 0; cnt < ksv_count; cnt++) {
297                 rev_ksv_list = srm_data->revoked_ksv_list;
298                 for (i = 0; i < rev_ksv_cnt; i++) {
299                         for (j = 0; j < DRM_HDCP_KSV_LEN; j++)
300                                 if (ksvs[j] != rev_ksv_list[j]) {
301                                         break;
302                                 } else if (j == (DRM_HDCP_KSV_LEN - 1)) {
303                                         DRM_DEBUG("Revoked KSV is ");
304                                         drm_hdcp_print_ksv(ksvs);
305                                         mutex_unlock(&srm_data->mutex);
306                                         return true;
307                                 }
308                         /* Move the offset to next KSV in the revoked list */
309                         rev_ksv_list += DRM_HDCP_KSV_LEN;
310                 }
311
312                 /* Iterate to next ksv_offset */
313                 ksvs += DRM_HDCP_KSV_LEN;
314         }
315         mutex_unlock(&srm_data->mutex);
316         return false;
317 }
318 EXPORT_SYMBOL_GPL(drm_hdcp_check_ksvs_revoked);
319
320 int drm_setup_hdcp_srm(struct class *drm_class)
321 {
322         srm_data = kzalloc(sizeof(*srm_data), GFP_KERNEL);
323         if (!srm_data)
324                 return -ENOMEM;
325         mutex_init(&srm_data->mutex);
326
327         return 0;
328 }
329
330 void drm_teardown_hdcp_srm(struct class *drm_class)
331 {
332         if (srm_data) {
333                 kfree(srm_data->revoked_ksv_list);
334                 kfree(srm_data);
335         }
336 }
337
338 static struct drm_prop_enum_list drm_cp_enum_list[] = {
339         { DRM_MODE_CONTENT_PROTECTION_UNDESIRED, "Undesired" },
340         { DRM_MODE_CONTENT_PROTECTION_DESIRED, "Desired" },
341         { DRM_MODE_CONTENT_PROTECTION_ENABLED, "Enabled" },
342 };
343 DRM_ENUM_NAME_FN(drm_get_content_protection_name, drm_cp_enum_list)
344
345 /**
346  * drm_connector_attach_content_protection_property - attach content protection
347  * property
348  *
349  * @connector: connector to attach CP property on.
350  *
351  * This is used to add support for content protection on select connectors.
352  * Content Protection is intentionally vague to allow for different underlying
353  * technologies, however it is most implemented by HDCP.
354  *
355  * The content protection will be set to &drm_connector_state.content_protection
356  *
357  * Returns:
358  * Zero on success, negative errno on failure.
359  */
360 int drm_connector_attach_content_protection_property(
361                 struct drm_connector *connector)
362 {
363         struct drm_device *dev = connector->dev;
364         struct drm_property *prop =
365                         dev->mode_config.content_protection_property;
366
367         if (!prop)
368                 prop = drm_property_create_enum(dev, 0, "Content Protection",
369                                                 drm_cp_enum_list,
370                                                 ARRAY_SIZE(drm_cp_enum_list));
371         if (!prop)
372                 return -ENOMEM;
373
374         drm_object_attach_property(&connector->base, prop,
375                                    DRM_MODE_CONTENT_PROTECTION_UNDESIRED);
376         dev->mode_config.content_protection_property = prop;
377
378         return 0;
379 }
380 EXPORT_SYMBOL(drm_connector_attach_content_protection_property);