linux/drivers/gpu/drm/i915/intel_guc_loader.c
<<
>>
Prefs
   1/*
   2 * Copyright © 2014 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 * Authors:
  24 *    Vinit Azad <vinit.azad@intel.com>
  25 *    Ben Widawsky <ben@bwidawsk.net>
  26 *    Dave Gordon <david.s.gordon@intel.com>
  27 *    Alex Dai <yu.dai@intel.com>
  28 */
  29#include "i915_drv.h"
  30#include "intel_uc.h"
  31
  32/**
  33 * DOC: GuC-specific firmware loader
  34 *
  35 * intel_guc:
  36 * Top level structure of guc. It handles firmware loading and manages client
  37 * pool and doorbells. intel_guc owns a i915_guc_client to replace the legacy
  38 * ExecList submission.
  39 *
  40 * Firmware versioning:
  41 * The firmware build process will generate a version header file with major and
  42 * minor version defined. The versions are built into CSS header of firmware.
  43 * i915 kernel driver set the minimal firmware version required per platform.
  44 * The firmware installation package will install (symbolic link) proper version
  45 * of firmware.
  46 *
  47 * GuC address space:
  48 * GuC does not allow any gfx GGTT address that falls into range [0, WOPCM_TOP),
  49 * which is reserved for Boot ROM, SRAM and WOPCM. Currently this top address is
  50 * 512K. In order to exclude 0-512K address space from GGTT, all gfx objects
  51 * used by GuC is pinned with PIN_OFFSET_BIAS along with size of WOPCM.
  52 *
  53 */
  54
  55#define SKL_FW_MAJOR 6
  56#define SKL_FW_MINOR 1
  57
  58#define BXT_FW_MAJOR 8
  59#define BXT_FW_MINOR 7
  60
  61#define KBL_FW_MAJOR 9
  62#define KBL_FW_MINOR 14
  63
  64#define GLK_FW_MAJOR 10
  65#define GLK_FW_MINOR 56
  66
  67#define GUC_FW_PATH(platform, major, minor) \
  68       "i915/" __stringify(platform) "_guc_ver" __stringify(major) "_" __stringify(minor) ".bin"
  69
  70#define I915_SKL_GUC_UCODE GUC_FW_PATH(skl, SKL_FW_MAJOR, SKL_FW_MINOR)
  71MODULE_FIRMWARE(I915_SKL_GUC_UCODE);
  72
  73#define I915_BXT_GUC_UCODE GUC_FW_PATH(bxt, BXT_FW_MAJOR, BXT_FW_MINOR)
  74MODULE_FIRMWARE(I915_BXT_GUC_UCODE);
  75
  76#define I915_KBL_GUC_UCODE GUC_FW_PATH(kbl, KBL_FW_MAJOR, KBL_FW_MINOR)
  77MODULE_FIRMWARE(I915_KBL_GUC_UCODE);
  78
  79#define I915_GLK_GUC_UCODE GUC_FW_PATH(glk, GLK_FW_MAJOR, GLK_FW_MINOR)
  80
  81
  82static u32 get_gttype(struct drm_i915_private *dev_priv)
  83{
  84        /* XXX: GT type based on PCI device ID? field seems unused by fw */
  85        return 0;
  86}
  87
  88static u32 get_core_family(struct drm_i915_private *dev_priv)
  89{
  90        u32 gen = INTEL_GEN(dev_priv);
  91
  92        switch (gen) {
  93        case 9:
  94                return GUC_CORE_FAMILY_GEN9;
  95
  96        default:
  97                MISSING_CASE(gen);
  98                return GUC_CORE_FAMILY_UNKNOWN;
  99        }
 100}
 101
 102/*
 103 * Initialise the GuC parameter block before starting the firmware
 104 * transfer. These parameters are read by the firmware on startup
 105 * and cannot be changed thereafter.
 106 */
 107static void guc_params_init(struct drm_i915_private *dev_priv)
 108{
 109        struct intel_guc *guc = &dev_priv->guc;
 110        u32 params[GUC_CTL_MAX_DWORDS];
 111        int i;
 112
 113        memset(&params, 0, sizeof(params));
 114
 115        params[GUC_CTL_DEVICE_INFO] |=
 116                (get_gttype(dev_priv) << GUC_CTL_GTTYPE_SHIFT) |
 117                (get_core_family(dev_priv) << GUC_CTL_COREFAMILY_SHIFT);
 118
 119        /*
 120         * GuC ARAT increment is 10 ns. GuC default scheduler quantum is one
 121         * second. This ARAR is calculated by:
 122         * Scheduler-Quantum-in-ns / ARAT-increment-in-ns = 1000000000 / 10
 123         */
 124        params[GUC_CTL_ARAT_HIGH] = 0;
 125        params[GUC_CTL_ARAT_LOW] = 100000000;
 126
 127        params[GUC_CTL_WA] |= GUC_CTL_WA_UK_BY_DRIVER;
 128
 129        params[GUC_CTL_FEATURE] |= GUC_CTL_DISABLE_SCHEDULER |
 130                        GUC_CTL_VCS2_ENABLED;
 131
 132        params[GUC_CTL_LOG_PARAMS] = guc->log.flags;
 133
 134        if (i915.guc_log_level >= 0) {
 135                params[GUC_CTL_DEBUG] =
 136                        i915.guc_log_level << GUC_LOG_VERBOSITY_SHIFT;
 137        } else
 138                params[GUC_CTL_DEBUG] = GUC_LOG_DISABLED;
 139
 140        /* If GuC submission is enabled, set up additional parameters here */
 141        if (i915.enable_guc_submission) {
 142                u32 ads = guc_ggtt_offset(guc->ads_vma) >> PAGE_SHIFT;
 143                u32 pgs = guc_ggtt_offset(dev_priv->guc.stage_desc_pool);
 144                u32 ctx_in_16 = GUC_MAX_STAGE_DESCRIPTORS / 16;
 145
 146                params[GUC_CTL_DEBUG] |= ads << GUC_ADS_ADDR_SHIFT;
 147                params[GUC_CTL_DEBUG] |= GUC_ADS_ENABLED;
 148
 149                pgs >>= PAGE_SHIFT;
 150                params[GUC_CTL_CTXINFO] = (pgs << GUC_CTL_BASE_ADDR_SHIFT) |
 151                        (ctx_in_16 << GUC_CTL_CTXNUM_IN16_SHIFT);
 152
 153                params[GUC_CTL_FEATURE] |= GUC_CTL_KERNEL_SUBMISSIONS;
 154
 155                /* Unmask this bit to enable the GuC's internal scheduler */
 156                params[GUC_CTL_FEATURE] &= ~GUC_CTL_DISABLE_SCHEDULER;
 157        }
 158
 159        I915_WRITE(SOFT_SCRATCH(0), 0);
 160
 161        for (i = 0; i < GUC_CTL_MAX_DWORDS; i++)
 162                I915_WRITE(SOFT_SCRATCH(1 + i), params[i]);
 163}
 164
 165/*
 166 * Read the GuC status register (GUC_STATUS) and store it in the
 167 * specified location; then return a boolean indicating whether
 168 * the value matches either of two values representing completion
 169 * of the GuC boot process.
 170 *
 171 * This is used for polling the GuC status in a wait_for()
 172 * loop below.
 173 */
 174static inline bool guc_ucode_response(struct drm_i915_private *dev_priv,
 175                                      u32 *status)
 176{
 177        u32 val = I915_READ(GUC_STATUS);
 178        u32 uk_val = val & GS_UKERNEL_MASK;
 179        *status = val;
 180        return (uk_val == GS_UKERNEL_READY ||
 181                ((val & GS_MIA_CORE_STATE) && uk_val == GS_UKERNEL_LAPIC_DONE));
 182}
 183
 184/*
 185 * Transfer the firmware image to RAM for execution by the microcontroller.
 186 *
 187 * Architecturally, the DMA engine is bidirectional, and can potentially even
 188 * transfer between GTT locations. This functionality is left out of the API
 189 * for now as there is no need for it.
 190 *
 191 * Note that GuC needs the CSS header plus uKernel code to be copied by the
 192 * DMA engine in one operation, whereas the RSA signature is loaded via MMIO.
 193 */
 194static int guc_ucode_xfer_dma(struct drm_i915_private *dev_priv,
 195                              struct i915_vma *vma)
 196{
 197        struct intel_uc_fw *guc_fw = &dev_priv->guc.fw;
 198        unsigned long offset;
 199        struct sg_table *sg = vma->pages;
 200        u32 status, rsa[UOS_RSA_SCRATCH_MAX_COUNT];
 201        int i, ret = 0;
 202
 203        /* where RSA signature starts */
 204        offset = guc_fw->rsa_offset;
 205
 206        /* Copy RSA signature from the fw image to HW for verification */
 207        sg_pcopy_to_buffer(sg->sgl, sg->nents, rsa, sizeof(rsa), offset);
 208        for (i = 0; i < UOS_RSA_SCRATCH_MAX_COUNT; i++)
 209                I915_WRITE(UOS_RSA_SCRATCH(i), rsa[i]);
 210
 211        /* The header plus uCode will be copied to WOPCM via DMA, excluding any
 212         * other components */
 213        I915_WRITE(DMA_COPY_SIZE, guc_fw->header_size + guc_fw->ucode_size);
 214
 215        /* Set the source address for the new blob */
 216        offset = guc_ggtt_offset(vma) + guc_fw->header_offset;
 217        I915_WRITE(DMA_ADDR_0_LOW, lower_32_bits(offset));
 218        I915_WRITE(DMA_ADDR_0_HIGH, upper_32_bits(offset) & 0xFFFF);
 219
 220        /*
 221         * Set the DMA destination. Current uCode expects the code to be
 222         * loaded at 8k; locations below this are used for the stack.
 223         */
 224        I915_WRITE(DMA_ADDR_1_LOW, 0x2000);
 225        I915_WRITE(DMA_ADDR_1_HIGH, DMA_ADDRESS_SPACE_WOPCM);
 226
 227        /* Finally start the DMA */
 228        I915_WRITE(DMA_CTRL, _MASKED_BIT_ENABLE(UOS_MOVE | START_DMA));
 229
 230        /*
 231         * Wait for the DMA to complete & the GuC to start up.
 232         * NB: Docs recommend not using the interrupt for completion.
 233         * Measurements indicate this should take no more than 20ms, so a
 234         * timeout here indicates that the GuC has failed and is unusable.
 235         * (Higher levels of the driver will attempt to fall back to
 236         * execlist mode if this happens.)
 237         */
 238        ret = wait_for(guc_ucode_response(dev_priv, &status), 100);
 239
 240        DRM_DEBUG_DRIVER("DMA status 0x%x, GuC status 0x%x\n",
 241                        I915_READ(DMA_CTRL), status);
 242
 243        if ((status & GS_BOOTROM_MASK) == GS_BOOTROM_RSA_FAILED) {
 244                DRM_ERROR("GuC firmware signature verification failed\n");
 245                ret = -ENOEXEC;
 246        }
 247
 248        DRM_DEBUG_DRIVER("returning %d\n", ret);
 249
 250        return ret;
 251}
 252
 253u32 intel_guc_wopcm_size(struct drm_i915_private *dev_priv)
 254{
 255        u32 wopcm_size = GUC_WOPCM_TOP;
 256
 257        /* On BXT, the top of WOPCM is reserved for RC6 context */
 258        if (IS_GEN9_LP(dev_priv))
 259                wopcm_size -= BXT_GUC_WOPCM_RC6_RESERVED;
 260
 261        return wopcm_size;
 262}
 263
 264/*
 265 * Load the GuC firmware blob into the MinuteIA.
 266 */
 267static int guc_ucode_xfer(struct drm_i915_private *dev_priv)
 268{
 269        struct intel_uc_fw *guc_fw = &dev_priv->guc.fw;
 270        struct i915_vma *vma;
 271        int ret;
 272
 273        ret = i915_gem_object_set_to_gtt_domain(guc_fw->obj, false);
 274        if (ret) {
 275                DRM_DEBUG_DRIVER("set-domain failed %d\n", ret);
 276                return ret;
 277        }
 278
 279        vma = i915_gem_object_ggtt_pin(guc_fw->obj, NULL, 0, 0,
 280                                       PIN_OFFSET_BIAS | GUC_WOPCM_TOP);
 281        if (IS_ERR(vma)) {
 282                DRM_DEBUG_DRIVER("pin failed %d\n", (int)PTR_ERR(vma));
 283                return PTR_ERR(vma);
 284        }
 285
 286        intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
 287
 288        /* Enable MIA caching. GuC clock gating is disabled. */
 289        I915_WRITE(GUC_SHIM_CONTROL, GUC_SHIM_CONTROL_VALUE);
 290
 291        /* WaDisableMinuteIaClockGating:bxt */
 292        if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_A1)) {
 293                I915_WRITE(GUC_SHIM_CONTROL, (I915_READ(GUC_SHIM_CONTROL) &
 294                                              ~GUC_ENABLE_MIA_CLOCK_GATING));
 295        }
 296
 297        /* WaC6DisallowByGfxPause:bxt */
 298        if (IS_BXT_REVID(dev_priv, 0, BXT_REVID_B0))
 299                I915_WRITE(GEN6_GFXPAUSE, 0x30FFF);
 300
 301        if (IS_GEN9_LP(dev_priv))
 302                I915_WRITE(GEN9LP_GT_PM_CONFIG, GT_DOORBELL_ENABLE);
 303        else
 304                I915_WRITE(GEN9_GT_PM_CONFIG, GT_DOORBELL_ENABLE);
 305
 306        if (IS_GEN9(dev_priv)) {
 307                /* DOP Clock Gating Enable for GuC clocks */
 308                I915_WRITE(GEN7_MISCCPCTL, (GEN8_DOP_CLOCK_GATE_GUC_ENABLE |
 309                                            I915_READ(GEN7_MISCCPCTL)));
 310
 311                /* allows for 5us (in 10ns units) before GT can go to RC6 */
 312                I915_WRITE(GUC_ARAT_C6DIS, 0x1FF);
 313        }
 314
 315        guc_params_init(dev_priv);
 316
 317        ret = guc_ucode_xfer_dma(dev_priv, vma);
 318
 319        intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
 320
 321        /*
 322         * We keep the object pages for reuse during resume. But we can unpin it
 323         * now that DMA has completed, so it doesn't continue to take up space.
 324         */
 325        i915_vma_unpin(vma);
 326
 327        return ret;
 328}
 329
 330/**
 331 * intel_guc_init_hw() - finish preparing the GuC for activity
 332 * @guc: intel_guc structure
 333 *
 334 * Called during driver loading and also after a GPU reset.
 335 *
 336 * The main action required here it to load the GuC uCode into the device.
 337 * The firmware image should have already been fetched into memory by the
 338 * earlier call to intel_guc_init(), so here we need only check that
 339 * worked, and then transfer the image to the h/w.
 340 *
 341 * Return:      non-zero code on error
 342 */
 343int intel_guc_init_hw(struct intel_guc *guc)
 344{
 345        struct drm_i915_private *dev_priv = guc_to_i915(guc);
 346        const char *fw_path = guc->fw.path;
 347        int ret;
 348
 349        DRM_DEBUG_DRIVER("GuC fw status: path %s, fetch %s, load %s\n",
 350                fw_path,
 351                intel_uc_fw_status_repr(guc->fw.fetch_status),
 352                intel_uc_fw_status_repr(guc->fw.load_status));
 353
 354        if (guc->fw.fetch_status != INTEL_UC_FIRMWARE_SUCCESS)
 355                return -EIO;
 356
 357        guc->fw.load_status = INTEL_UC_FIRMWARE_PENDING;
 358
 359        DRM_DEBUG_DRIVER("GuC fw status: fetch %s, load %s\n",
 360                intel_uc_fw_status_repr(guc->fw.fetch_status),
 361                intel_uc_fw_status_repr(guc->fw.load_status));
 362
 363        ret = guc_ucode_xfer(dev_priv);
 364
 365        if (ret)
 366                return -EAGAIN;
 367
 368        guc->fw.load_status = INTEL_UC_FIRMWARE_SUCCESS;
 369
 370        DRM_INFO("GuC %s (firmware %s [version %u.%u])\n",
 371                 i915.enable_guc_submission ? "submission enabled" : "loaded",
 372                 guc->fw.path,
 373                 guc->fw.major_ver_found, guc->fw.minor_ver_found);
 374
 375        return 0;
 376}
 377
 378/**
 379 * intel_guc_select_fw() - selects GuC firmware for loading
 380 * @guc:        intel_guc struct
 381 *
 382 * Return: zero when we know firmware, non-zero in other case
 383 */
 384int intel_guc_select_fw(struct intel_guc *guc)
 385{
 386        struct drm_i915_private *dev_priv = guc_to_i915(guc);
 387
 388        guc->fw.path = NULL;
 389        guc->fw.fetch_status = INTEL_UC_FIRMWARE_NONE;
 390        guc->fw.load_status = INTEL_UC_FIRMWARE_NONE;
 391        guc->fw.type = INTEL_UC_FW_TYPE_GUC;
 392
 393        if (i915.guc_firmware_path) {
 394                guc->fw.path = i915.guc_firmware_path;
 395                guc->fw.major_ver_wanted = 0;
 396                guc->fw.minor_ver_wanted = 0;
 397        } else if (IS_SKYLAKE(dev_priv)) {
 398                guc->fw.path = I915_SKL_GUC_UCODE;
 399                guc->fw.major_ver_wanted = SKL_FW_MAJOR;
 400                guc->fw.minor_ver_wanted = SKL_FW_MINOR;
 401        } else if (IS_BROXTON(dev_priv)) {
 402                guc->fw.path = I915_BXT_GUC_UCODE;
 403                guc->fw.major_ver_wanted = BXT_FW_MAJOR;
 404                guc->fw.minor_ver_wanted = BXT_FW_MINOR;
 405        } else if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv)) {
 406                guc->fw.path = I915_KBL_GUC_UCODE;
 407                guc->fw.major_ver_wanted = KBL_FW_MAJOR;
 408                guc->fw.minor_ver_wanted = KBL_FW_MINOR;
 409        } else if (IS_GEMINILAKE(dev_priv)) {
 410                guc->fw.path = I915_GLK_GUC_UCODE;
 411                guc->fw.major_ver_wanted = GLK_FW_MAJOR;
 412                guc->fw.minor_ver_wanted = GLK_FW_MINOR;
 413        } else {
 414                DRM_ERROR("No GuC firmware known for platform with GuC!\n");
 415                return -ENOENT;
 416        }
 417
 418        return 0;
 419}
 420