Merge tag 'for-linus-20180713' of git://git.kernel.dk/linux-block
[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 /**
36  * intel_huc_auth() - Authenticate HuC uCode
37  * @huc: intel_huc structure
38  *
39  * Called after HuC and GuC firmware loading during intel_uc_init_hw().
40  *
41  * This function pins HuC firmware image object into GGTT.
42  * Then it invokes GuC action to authenticate passing the offset to RSA
43  * signature through intel_guc_auth_huc(). It then waits for 50ms for
44  * firmware verification ACK and unpins the object.
45  */
46 int intel_huc_auth(struct intel_huc *huc)
47 {
48         struct drm_i915_private *i915 = huc_to_i915(huc);
49         struct intel_guc *guc = &i915->guc;
50         struct i915_vma *vma;
51         u32 status;
52         int ret;
53
54         if (huc->fw.load_status != INTEL_UC_FIRMWARE_SUCCESS)
55                 return -ENOEXEC;
56
57         vma = i915_gem_object_ggtt_pin(huc->fw.obj, NULL, 0, 0,
58                                        PIN_OFFSET_BIAS | guc->ggtt_pin_bias);
59         if (IS_ERR(vma)) {
60                 ret = PTR_ERR(vma);
61                 DRM_ERROR("HuC: Failed to pin huc fw object %d\n", ret);
62                 goto fail;
63         }
64
65         ret = intel_guc_auth_huc(guc,
66                                  intel_guc_ggtt_offset(guc, vma) +
67                                  huc->fw.rsa_offset);
68         if (ret) {
69                 DRM_ERROR("HuC: GuC did not ack Auth request %d\n", ret);
70                 goto fail_unpin;
71         }
72
73         /* Check authentication status, it should be done by now */
74         ret = __intel_wait_for_register(i915,
75                                         HUC_STATUS2,
76                                         HUC_FW_VERIFIED,
77                                         HUC_FW_VERIFIED,
78                                         2, 50, &status);
79         if (ret) {
80                 DRM_ERROR("HuC: Firmware not verified %#x\n", status);
81                 goto fail_unpin;
82         }
83
84         i915_vma_unpin(vma);
85         return 0;
86
87 fail_unpin:
88         i915_vma_unpin(vma);
89 fail:
90         huc->fw.load_status = INTEL_UC_FIRMWARE_FAIL;
91
92         DRM_ERROR("HuC: Authentication failed %d\n", ret);
93         return ret;
94 }
95
96 /**
97  * intel_huc_check_status() - check HuC status
98  * @huc: intel_huc structure
99  *
100  * This function reads status register to verify if HuC
101  * firmware was successfully loaded.
102  *
103  * Returns positive value if HuC firmware is loaded and verified
104  * and -ENODEV if HuC is not present.
105  */
106 int intel_huc_check_status(struct intel_huc *huc)
107 {
108         struct drm_i915_private *dev_priv = huc_to_i915(huc);
109         u32 status;
110
111         if (!HAS_HUC(dev_priv))
112                 return -ENODEV;
113
114         intel_runtime_pm_get(dev_priv);
115         status = I915_READ(HUC_STATUS2) & HUC_FW_VERIFIED;
116         intel_runtime_pm_put(dev_priv);
117
118         return status;
119 }