Merge tag 'char-misc-5.4-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[linux-2.6-microblaze.git] / drivers / gpu / drm / i915 / intel_huc.c
1 /*
2  * Copyright © 2016-2017 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  */
24
25 #include <linux/types.h>
26
27 #include "intel_huc.h"
28 #include "i915_drv.h"
29
30 void intel_huc_init_early(struct intel_huc *huc)
31 {
32         struct drm_i915_private *i915 = huc_to_i915(huc);
33
34         intel_huc_fw_init_early(huc);
35
36         if (INTEL_GEN(i915) >= 11) {
37                 huc->status.reg = GEN11_HUC_KERNEL_LOAD_INFO;
38                 huc->status.mask = HUC_LOAD_SUCCESSFUL;
39                 huc->status.value = HUC_LOAD_SUCCESSFUL;
40         } else {
41                 huc->status.reg = HUC_STATUS2;
42                 huc->status.mask = HUC_FW_VERIFIED;
43                 huc->status.value = HUC_FW_VERIFIED;
44         }
45 }
46
47 int intel_huc_init_misc(struct intel_huc *huc)
48 {
49         struct drm_i915_private *i915 = huc_to_i915(huc);
50
51         intel_uc_fw_fetch(i915, &huc->fw);
52         return 0;
53 }
54
55 static int intel_huc_rsa_data_create(struct intel_huc *huc)
56 {
57         struct drm_i915_private *i915 = huc_to_i915(huc);
58         struct intel_guc *guc = &i915->guc;
59         struct i915_vma *vma;
60         void *vaddr;
61
62         /*
63          * HuC firmware will sit above GUC_GGTT_TOP and will not map
64          * through GTT. Unfortunately, this means GuC cannot perform
65          * the HuC auth. as the rsa offset now falls within the GuC
66          * inaccessible range. We resort to perma-pinning an additional
67          * vma within the accessible range that only contains the rsa
68          * signature. The GuC can use this extra pinning to perform
69          * the authentication since its GGTT offset will be GuC
70          * accessible.
71          */
72         vma = intel_guc_allocate_vma(guc, PAGE_SIZE);
73         if (IS_ERR(vma))
74                 return PTR_ERR(vma);
75
76         vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
77         if (IS_ERR(vaddr)) {
78                 i915_vma_unpin_and_release(&vma, 0);
79                 return PTR_ERR(vaddr);
80         }
81
82         huc->rsa_data = vma;
83         huc->rsa_data_vaddr = vaddr;
84
85         return 0;
86 }
87
88 static void intel_huc_rsa_data_destroy(struct intel_huc *huc)
89 {
90         i915_vma_unpin_and_release(&huc->rsa_data, I915_VMA_RELEASE_MAP);
91 }
92
93 int intel_huc_init(struct intel_huc *huc)
94 {
95         int err;
96
97         err = intel_huc_rsa_data_create(huc);
98         if (err)
99                 return err;
100
101         return intel_uc_fw_init(&huc->fw);
102 }
103
104 void intel_huc_fini(struct intel_huc *huc)
105 {
106         intel_uc_fw_fini(&huc->fw);
107         intel_huc_rsa_data_destroy(huc);
108 }
109
110 /**
111  * intel_huc_auth() - Authenticate HuC uCode
112  * @huc: intel_huc structure
113  *
114  * Called after HuC and GuC firmware loading during intel_uc_init_hw().
115  *
116  * This function pins HuC firmware image object into GGTT.
117  * Then it invokes GuC action to authenticate passing the offset to RSA
118  * signature through intel_guc_auth_huc(). It then waits for 50ms for
119  * firmware verification ACK and unpins the object.
120  */
121 int intel_huc_auth(struct intel_huc *huc)
122 {
123         struct drm_i915_private *i915 = huc_to_i915(huc);
124         struct intel_guc *guc = &i915->guc;
125         int ret;
126
127         if (huc->fw.load_status != INTEL_UC_FIRMWARE_SUCCESS)
128                 return -ENOEXEC;
129
130         ret = intel_guc_auth_huc(guc,
131                                  intel_guc_ggtt_offset(guc, huc->rsa_data));
132         if (ret) {
133                 DRM_ERROR("HuC: GuC did not ack Auth request %d\n", ret);
134                 goto fail;
135         }
136
137         /* Check authentication status, it should be done by now */
138         ret = __intel_wait_for_register(&i915->uncore,
139                                         huc->status.reg,
140                                         huc->status.mask,
141                                         huc->status.value,
142                                         2, 50, NULL);
143         if (ret) {
144                 DRM_ERROR("HuC: Firmware not verified %d\n", ret);
145                 goto fail;
146         }
147
148         return 0;
149
150 fail:
151         huc->fw.load_status = INTEL_UC_FIRMWARE_FAIL;
152
153         DRM_ERROR("HuC: Authentication failed %d\n", ret);
154         return ret;
155 }
156
157 /**
158  * intel_huc_check_status() - check HuC status
159  * @huc: intel_huc structure
160  *
161  * This function reads status register to verify if HuC
162  * firmware was successfully loaded.
163  *
164  * Returns: 1 if HuC firmware is loaded and verified,
165  * 0 if HuC firmware is not loaded and -ENODEV if HuC
166  * is not present on this platform.
167  */
168 int intel_huc_check_status(struct intel_huc *huc)
169 {
170         struct drm_i915_private *dev_priv = huc_to_i915(huc);
171         intel_wakeref_t wakeref;
172         bool status = false;
173
174         if (!HAS_HUC(dev_priv))
175                 return -ENODEV;
176
177         with_intel_runtime_pm(&dev_priv->runtime_pm, wakeref)
178                 status = (I915_READ(huc->status.reg) & huc->status.mask) ==
179                           huc->status.value;
180
181         return status;
182 }