Merge tag 'drm-intel-next-2019-05-24' of git://anongit.freedesktop.org/drm/drm-intel...
[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         intel_huc_fw_init_early(huc);
33 }
34
35 int intel_huc_init_misc(struct intel_huc *huc)
36 {
37         struct drm_i915_private *i915 = huc_to_i915(huc);
38
39         intel_uc_fw_fetch(i915, &huc->fw);
40         return 0;
41 }
42
43 static int intel_huc_rsa_data_create(struct intel_huc *huc)
44 {
45         struct drm_i915_private *i915 = huc_to_i915(huc);
46         struct intel_guc *guc = &i915->guc;
47         struct i915_vma *vma;
48         void *vaddr;
49
50         /*
51          * HuC firmware will sit above GUC_GGTT_TOP and will not map
52          * through GTT. Unfortunately, this means GuC cannot perform
53          * the HuC auth. as the rsa offset now falls within the GuC
54          * inaccessible range. We resort to perma-pinning an additional
55          * vma within the accessible range that only contains the rsa
56          * signature. The GuC can use this extra pinning to perform
57          * the authentication since its GGTT offset will be GuC
58          * accessible.
59          */
60         vma = intel_guc_allocate_vma(guc, PAGE_SIZE);
61         if (IS_ERR(vma))
62                 return PTR_ERR(vma);
63
64         vaddr = i915_gem_object_pin_map(vma->obj, I915_MAP_WB);
65         if (IS_ERR(vaddr)) {
66                 i915_vma_unpin_and_release(&vma, 0);
67                 return PTR_ERR(vaddr);
68         }
69
70         huc->rsa_data = vma;
71         huc->rsa_data_vaddr = vaddr;
72
73         return 0;
74 }
75
76 static void intel_huc_rsa_data_destroy(struct intel_huc *huc)
77 {
78         i915_vma_unpin_and_release(&huc->rsa_data, I915_VMA_RELEASE_MAP);
79 }
80
81 int intel_huc_init(struct intel_huc *huc)
82 {
83         int err;
84
85         err = intel_huc_rsa_data_create(huc);
86         if (err)
87                 return err;
88
89         return intel_uc_fw_init(&huc->fw);
90 }
91
92 void intel_huc_fini(struct intel_huc *huc)
93 {
94         intel_uc_fw_fini(&huc->fw);
95         intel_huc_rsa_data_destroy(huc);
96 }
97
98 /**
99  * intel_huc_auth() - Authenticate HuC uCode
100  * @huc: intel_huc structure
101  *
102  * Called after HuC and GuC firmware loading during intel_uc_init_hw().
103  *
104  * This function pins HuC firmware image object into GGTT.
105  * Then it invokes GuC action to authenticate passing the offset to RSA
106  * signature through intel_guc_auth_huc(). It then waits for 50ms for
107  * firmware verification ACK and unpins the object.
108  */
109 int intel_huc_auth(struct intel_huc *huc)
110 {
111         struct drm_i915_private *i915 = huc_to_i915(huc);
112         struct intel_guc *guc = &i915->guc;
113         u32 status;
114         int ret;
115
116         if (huc->fw.load_status != INTEL_UC_FIRMWARE_SUCCESS)
117                 return -ENOEXEC;
118
119         ret = intel_guc_auth_huc(guc,
120                                  intel_guc_ggtt_offset(guc, huc->rsa_data));
121         if (ret) {
122                 DRM_ERROR("HuC: GuC did not ack Auth request %d\n", ret);
123                 goto fail;
124         }
125
126         /* Check authentication status, it should be done by now */
127         ret = __intel_wait_for_register(&i915->uncore,
128                                         HUC_STATUS2,
129                                         HUC_FW_VERIFIED,
130                                         HUC_FW_VERIFIED,
131                                         2, 50, &status);
132         if (ret) {
133                 DRM_ERROR("HuC: Firmware not verified %#x\n", status);
134                 goto fail;
135         }
136
137         return 0;
138
139 fail:
140         huc->fw.load_status = INTEL_UC_FIRMWARE_FAIL;
141
142         DRM_ERROR("HuC: Authentication failed %d\n", ret);
143         return ret;
144 }
145
146 /**
147  * intel_huc_check_status() - check HuC status
148  * @huc: intel_huc structure
149  *
150  * This function reads status register to verify if HuC
151  * firmware was successfully loaded.
152  *
153  * Returns: 1 if HuC firmware is loaded and verified,
154  * 0 if HuC firmware is not loaded and -ENODEV if HuC
155  * is not present on this platform.
156  */
157 int intel_huc_check_status(struct intel_huc *huc)
158 {
159         struct drm_i915_private *dev_priv = huc_to_i915(huc);
160         intel_wakeref_t wakeref;
161         bool status = false;
162
163         if (!HAS_HUC(dev_priv))
164                 return -ENODEV;
165
166         with_intel_runtime_pm(dev_priv, wakeref)
167                 status = I915_READ(HUC_STATUS2) & HUC_FW_VERIFIED;
168
169         return status;
170 }