]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/i915/intel_uc_fw.c
eca741a857a5cab4354cc82fe160b6e3fbed83f9
[linux.git] / drivers / gpu / drm / i915 / intel_uc_fw.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/bitfield.h>
26 #include <linux/firmware.h>
27 #include <drm/drm_print.h>
28
29 #include "intel_uc_fw.h"
30 #include "i915_drv.h"
31
32 /**
33  * intel_uc_fw_fetch - fetch uC firmware
34  *
35  * @dev_priv: device private
36  * @uc_fw: uC firmware
37  *
38  * Fetch uC firmware into GEM obj.
39  */
40 void intel_uc_fw_fetch(struct drm_i915_private *dev_priv,
41                        struct intel_uc_fw *uc_fw)
42 {
43         struct pci_dev *pdev = dev_priv->drm.pdev;
44         struct drm_i915_gem_object *obj;
45         const struct firmware *fw = NULL;
46         struct uc_css_header *css;
47         size_t size;
48         int err;
49
50         if (!uc_fw->path) {
51                 dev_info(dev_priv->drm.dev,
52                          "%s: No firmware was defined for %s!\n",
53                          intel_uc_fw_type_repr(uc_fw->type),
54                          intel_platform_name(INTEL_INFO(dev_priv)->platform));
55                 return;
56         }
57
58         DRM_DEBUG_DRIVER("%s fw fetch %s\n",
59                          intel_uc_fw_type_repr(uc_fw->type), uc_fw->path);
60
61         uc_fw->fetch_status = INTEL_UC_FIRMWARE_PENDING;
62         DRM_DEBUG_DRIVER("%s fw fetch %s\n",
63                          intel_uc_fw_type_repr(uc_fw->type),
64                          intel_uc_fw_status_repr(uc_fw->fetch_status));
65
66         err = request_firmware(&fw, uc_fw->path, &pdev->dev);
67         if (err) {
68                 DRM_DEBUG_DRIVER("%s fw request_firmware err=%d\n",
69                                  intel_uc_fw_type_repr(uc_fw->type), err);
70                 goto fail;
71         }
72
73         DRM_DEBUG_DRIVER("%s fw size %zu ptr %p\n",
74                          intel_uc_fw_type_repr(uc_fw->type), fw->size, fw);
75
76         /* Check the size of the blob before examining buffer contents */
77         if (fw->size < sizeof(struct uc_css_header)) {
78                 DRM_WARN("%s: Unexpected firmware size (%zu, min %zu)\n",
79                          intel_uc_fw_type_repr(uc_fw->type),
80                          fw->size, sizeof(struct uc_css_header));
81                 err = -ENODATA;
82                 goto fail;
83         }
84
85         css = (struct uc_css_header *)fw->data;
86
87         /* Firmware bits always start from header */
88         uc_fw->header_offset = 0;
89         uc_fw->header_size = (css->header_size_dw - css->modulus_size_dw -
90                               css->key_size_dw - css->exponent_size_dw) *
91                              sizeof(u32);
92
93         if (uc_fw->header_size != sizeof(struct uc_css_header)) {
94                 DRM_WARN("%s: Mismatched firmware header definition\n",
95                          intel_uc_fw_type_repr(uc_fw->type));
96                 err = -ENOEXEC;
97                 goto fail;
98         }
99
100         /* then, uCode */
101         uc_fw->ucode_offset = uc_fw->header_offset + uc_fw->header_size;
102         uc_fw->ucode_size = (css->size_dw - css->header_size_dw) * sizeof(u32);
103
104         /* now RSA */
105         if (css->key_size_dw != UOS_RSA_SCRATCH_COUNT) {
106                 DRM_WARN("%s: Mismatched firmware RSA key size (%u)\n",
107                          intel_uc_fw_type_repr(uc_fw->type), css->key_size_dw);
108                 err = -ENOEXEC;
109                 goto fail;
110         }
111         uc_fw->rsa_offset = uc_fw->ucode_offset + uc_fw->ucode_size;
112         uc_fw->rsa_size = css->key_size_dw * sizeof(u32);
113
114         /* At least, it should have header, uCode and RSA. Size of all three. */
115         size = uc_fw->header_size + uc_fw->ucode_size + uc_fw->rsa_size;
116         if (fw->size < size) {
117                 DRM_WARN("%s: Truncated firmware (%zu, expected %zu)\n",
118                          intel_uc_fw_type_repr(uc_fw->type), fw->size, size);
119                 err = -ENOEXEC;
120                 goto fail;
121         }
122
123         /* Get version numbers from the CSS header */
124         switch (uc_fw->type) {
125         case INTEL_UC_FW_TYPE_GUC:
126                 uc_fw->major_ver_found = FIELD_GET(CSS_SW_VERSION_GUC_MAJOR,
127                                                    css->sw_version);
128                 uc_fw->minor_ver_found = FIELD_GET(CSS_SW_VERSION_GUC_MINOR,
129                                                    css->sw_version);
130                 break;
131
132         case INTEL_UC_FW_TYPE_HUC:
133                 uc_fw->major_ver_found = FIELD_GET(CSS_SW_VERSION_HUC_MAJOR,
134                                                    css->sw_version);
135                 uc_fw->minor_ver_found = FIELD_GET(CSS_SW_VERSION_HUC_MINOR,
136                                                    css->sw_version);
137                 break;
138
139         default:
140                 MISSING_CASE(uc_fw->type);
141                 break;
142         }
143
144         DRM_DEBUG_DRIVER("%s fw version %u.%u (wanted %u.%u)\n",
145                          intel_uc_fw_type_repr(uc_fw->type),
146                          uc_fw->major_ver_found, uc_fw->minor_ver_found,
147                          uc_fw->major_ver_wanted, uc_fw->minor_ver_wanted);
148
149         if (uc_fw->major_ver_wanted == 0 && uc_fw->minor_ver_wanted == 0) {
150                 DRM_NOTE("%s: Skipping firmware version check\n",
151                          intel_uc_fw_type_repr(uc_fw->type));
152         } else if (uc_fw->major_ver_found != uc_fw->major_ver_wanted ||
153                    uc_fw->minor_ver_found < uc_fw->minor_ver_wanted) {
154                 DRM_NOTE("%s: Wrong firmware version (%u.%u, required %u.%u)\n",
155                          intel_uc_fw_type_repr(uc_fw->type),
156                          uc_fw->major_ver_found, uc_fw->minor_ver_found,
157                          uc_fw->major_ver_wanted, uc_fw->minor_ver_wanted);
158                 err = -ENOEXEC;
159                 goto fail;
160         }
161
162         obj = i915_gem_object_create_from_data(dev_priv, fw->data, fw->size);
163         if (IS_ERR(obj)) {
164                 err = PTR_ERR(obj);
165                 DRM_DEBUG_DRIVER("%s fw object_create err=%d\n",
166                                  intel_uc_fw_type_repr(uc_fw->type), err);
167                 goto fail;
168         }
169
170         uc_fw->obj = obj;
171         uc_fw->size = fw->size;
172         uc_fw->fetch_status = INTEL_UC_FIRMWARE_SUCCESS;
173         DRM_DEBUG_DRIVER("%s fw fetch %s\n",
174                          intel_uc_fw_type_repr(uc_fw->type),
175                          intel_uc_fw_status_repr(uc_fw->fetch_status));
176
177         release_firmware(fw);
178         return;
179
180 fail:
181         uc_fw->fetch_status = INTEL_UC_FIRMWARE_FAIL;
182         DRM_DEBUG_DRIVER("%s fw fetch %s\n",
183                          intel_uc_fw_type_repr(uc_fw->type),
184                          intel_uc_fw_status_repr(uc_fw->fetch_status));
185
186         DRM_WARN("%s: Failed to fetch firmware %s (error %d)\n",
187                  intel_uc_fw_type_repr(uc_fw->type), uc_fw->path, err);
188         DRM_INFO("%s: Firmware can be downloaded from %s\n",
189                  intel_uc_fw_type_repr(uc_fw->type), INTEL_UC_FIRMWARE_URL);
190
191         release_firmware(fw);           /* OK even if fw is NULL */
192 }
193
194 static void intel_uc_fw_ggtt_bind(struct intel_uc_fw *uc_fw)
195 {
196         struct drm_i915_gem_object *obj = uc_fw->obj;
197         struct i915_ggtt *ggtt = &to_i915(obj->base.dev)->ggtt;
198         struct i915_vma dummy = {
199                 .node.start = intel_uc_fw_ggtt_offset(uc_fw),
200                 .node.size = obj->base.size,
201                 .pages = obj->mm.pages,
202                 .vm = &ggtt->vm,
203         };
204
205         GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
206         GEM_BUG_ON(dummy.node.size > ggtt->uc_fw.size);
207
208         /* uc_fw->obj cache domains were not controlled across suspend */
209         drm_clflush_sg(dummy.pages);
210
211         ggtt->vm.insert_entries(&ggtt->vm, &dummy, I915_CACHE_NONE, 0);
212 }
213
214 static void intel_uc_fw_ggtt_unbind(struct intel_uc_fw *uc_fw)
215 {
216         struct drm_i915_gem_object *obj = uc_fw->obj;
217         struct i915_ggtt *ggtt = &to_i915(obj->base.dev)->ggtt;
218         u64 start = intel_uc_fw_ggtt_offset(uc_fw);
219
220         ggtt->vm.clear_range(&ggtt->vm, start, obj->base.size);
221 }
222
223 /**
224  * intel_uc_fw_upload - load uC firmware using custom loader
225  * @uc_fw: uC firmware
226  * @xfer: custom uC firmware loader function
227  *
228  * Loads uC firmware using custom loader and updates internal flags.
229  *
230  * Return: 0 on success, non-zero on failure.
231  */
232 int intel_uc_fw_upload(struct intel_uc_fw *uc_fw,
233                        int (*xfer)(struct intel_uc_fw *uc_fw))
234 {
235         int err;
236
237         DRM_DEBUG_DRIVER("%s fw load %s\n",
238                          intel_uc_fw_type_repr(uc_fw->type), uc_fw->path);
239
240         if (uc_fw->fetch_status != INTEL_UC_FIRMWARE_SUCCESS)
241                 return -ENOEXEC;
242
243         uc_fw->load_status = INTEL_UC_FIRMWARE_PENDING;
244         DRM_DEBUG_DRIVER("%s fw load %s\n",
245                          intel_uc_fw_type_repr(uc_fw->type),
246                          intel_uc_fw_status_repr(uc_fw->load_status));
247
248         intel_uc_fw_ggtt_bind(uc_fw);
249
250         /* Call custom loader */
251         err = xfer(uc_fw);
252         if (err)
253                 goto fail;
254
255         intel_uc_fw_ggtt_unbind(uc_fw);
256
257         uc_fw->load_status = INTEL_UC_FIRMWARE_SUCCESS;
258         DRM_DEBUG_DRIVER("%s fw load %s\n",
259                          intel_uc_fw_type_repr(uc_fw->type),
260                          intel_uc_fw_status_repr(uc_fw->load_status));
261
262         DRM_INFO("%s: Loaded firmware %s (version %u.%u)\n",
263                  intel_uc_fw_type_repr(uc_fw->type),
264                  uc_fw->path,
265                  uc_fw->major_ver_found, uc_fw->minor_ver_found);
266
267         return 0;
268
269 fail:
270         uc_fw->load_status = INTEL_UC_FIRMWARE_FAIL;
271         DRM_DEBUG_DRIVER("%s fw load %s\n",
272                          intel_uc_fw_type_repr(uc_fw->type),
273                          intel_uc_fw_status_repr(uc_fw->load_status));
274
275         DRM_WARN("%s: Failed to load firmware %s (error %d)\n",
276                  intel_uc_fw_type_repr(uc_fw->type), uc_fw->path, err);
277
278         return err;
279 }
280
281 int intel_uc_fw_init(struct intel_uc_fw *uc_fw)
282 {
283         int err;
284
285         if (uc_fw->fetch_status != INTEL_UC_FIRMWARE_SUCCESS)
286                 return -ENOEXEC;
287
288         err = i915_gem_object_pin_pages(uc_fw->obj);
289         if (err)
290                 DRM_DEBUG_DRIVER("%s fw pin-pages err=%d\n",
291                                  intel_uc_fw_type_repr(uc_fw->type), err);
292
293         return err;
294 }
295
296 void intel_uc_fw_fini(struct intel_uc_fw *uc_fw)
297 {
298         if (uc_fw->fetch_status != INTEL_UC_FIRMWARE_SUCCESS)
299                 return;
300
301         i915_gem_object_unpin_pages(uc_fw->obj);
302 }
303
304 u32 intel_uc_fw_ggtt_offset(struct intel_uc_fw *uc_fw)
305 {
306         struct drm_i915_private *i915 = to_i915(uc_fw->obj->base.dev);
307         struct i915_ggtt *ggtt = &i915->ggtt;
308         struct drm_mm_node *node = &ggtt->uc_fw;
309
310         GEM_BUG_ON(!node->allocated);
311         GEM_BUG_ON(upper_32_bits(node->start));
312         GEM_BUG_ON(upper_32_bits(node->start + node->size - 1));
313
314         return lower_32_bits(node->start);
315 }
316
317 /**
318  * intel_uc_fw_cleanup_fetch - cleanup uC firmware
319  *
320  * @uc_fw: uC firmware
321  *
322  * Cleans up uC firmware by releasing the firmware GEM obj.
323  */
324 void intel_uc_fw_cleanup_fetch(struct intel_uc_fw *uc_fw)
325 {
326         struct drm_i915_gem_object *obj;
327
328         obj = fetch_and_zero(&uc_fw->obj);
329         if (obj)
330                 i915_gem_object_put(obj);
331
332         uc_fw->fetch_status = INTEL_UC_FIRMWARE_NONE;
333 }
334
335 /**
336  * intel_uc_fw_dump - dump information about uC firmware
337  * @uc_fw: uC firmware
338  * @p: the &drm_printer
339  *
340  * Pretty printer for uC firmware.
341  */
342 void intel_uc_fw_dump(const struct intel_uc_fw *uc_fw, struct drm_printer *p)
343 {
344         drm_printf(p, "%s firmware: %s\n",
345                    intel_uc_fw_type_repr(uc_fw->type), uc_fw->path);
346         drm_printf(p, "\tstatus: fetch %s, load %s\n",
347                    intel_uc_fw_status_repr(uc_fw->fetch_status),
348                    intel_uc_fw_status_repr(uc_fw->load_status));
349         drm_printf(p, "\tversion: wanted %u.%u, found %u.%u\n",
350                    uc_fw->major_ver_wanted, uc_fw->minor_ver_wanted,
351                    uc_fw->major_ver_found, uc_fw->minor_ver_found);
352         drm_printf(p, "\theader: offset %u, size %u\n",
353                    uc_fw->header_offset, uc_fw->header_size);
354         drm_printf(p, "\tuCode: offset %u, size %u\n",
355                    uc_fw->ucode_offset, uc_fw->ucode_size);
356         drm_printf(p, "\tRSA: offset %u, size %u\n",
357                    uc_fw->rsa_offset, uc_fw->rsa_size);
358 }