Subversion Repositories Kolibri OS

Rev

Rev 6937 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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.  */
  24. #include <linux/firmware.h>
  25. #include <linux/circ_buf.h>
  26. #include "intel_drv.h"
  27. #include "i915_drv.h"
  28. #include "intel_guc.h"
  29.  
  30. /**
  31.  * DOC: GuC Client
  32.  *
  33.  * i915_guc_client:
  34.  * We use the term client to avoid confusion with contexts. A i915_guc_client is
  35.  * equivalent to GuC object guc_context_desc. This context descriptor is
  36.  * allocated from a pool of 1024 entries. Kernel driver will allocate doorbell
  37.  * and workqueue for it. Also the process descriptor (guc_process_desc), which
  38.  * is mapped to client space. So the client can write Work Item then ring the
  39.  * doorbell.
  40.  *
  41.  * To simplify the implementation, we allocate one gem object that contains all
  42.  * pages for doorbell, process descriptor and workqueue.
  43.  *
  44.  * The Scratch registers:
  45.  * There are 16 MMIO-based registers start from 0xC180. The kernel driver writes
  46.  * a value to the action register (SOFT_SCRATCH_0) along with any data. It then
  47.  * triggers an interrupt on the GuC via another register write (0xC4C8).
  48.  * Firmware writes a success/fail code back to the action register after
  49.  * processes the request. The kernel driver polls waiting for this update and
  50.  * then proceeds.
  51.  * See host2guc_action()
  52.  *
  53.  * Doorbells:
  54.  * Doorbells are interrupts to uKernel. A doorbell is a single cache line (QW)
  55.  * mapped into process space.
  56.  *
  57.  * Work Items:
  58.  * There are several types of work items that the host may place into a
  59.  * workqueue, each with its own requirements and limitations. Currently only
  60.  * WQ_TYPE_INORDER is needed to support legacy submission via GuC, which
  61.  * represents in-order queue. The kernel driver packs ring tail pointer and an
  62.  * ELSP context descriptor dword into Work Item.
  63.  * See guc_add_workqueue_item()
  64.  *
  65.  */
  66.  
  67. /*
  68.  * Read GuC command/status register (SOFT_SCRATCH_0)
  69.  * Return true if it contains a response rather than a command
  70.  */
  71. static inline bool host2guc_action_response(struct drm_i915_private *dev_priv,
  72.                                             u32 *status)
  73. {
  74.         u32 val = I915_READ(SOFT_SCRATCH(0));
  75.         *status = val;
  76.         return GUC2HOST_IS_RESPONSE(val);
  77. }
  78.  
  79. static int host2guc_action(struct intel_guc *guc, u32 *data, u32 len)
  80. {
  81.         struct drm_i915_private *dev_priv = guc_to_i915(guc);
  82.         u32 status;
  83.         int i;
  84.         int ret;
  85.  
  86.         if (WARN_ON(len < 1 || len > 15))
  87.                 return -EINVAL;
  88.  
  89.         intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  90.         spin_lock(&dev_priv->guc.host2guc_lock);
  91.  
  92.         dev_priv->guc.action_count += 1;
  93.         dev_priv->guc.action_cmd = data[0];
  94.  
  95.         for (i = 0; i < len; i++)
  96.                 I915_WRITE(SOFT_SCRATCH(i), data[i]);
  97.  
  98.         POSTING_READ(SOFT_SCRATCH(i - 1));
  99.  
  100.         I915_WRITE(HOST2GUC_INTERRUPT, HOST2GUC_TRIGGER);
  101.  
  102.         /* No HOST2GUC command should take longer than 10ms */
  103.         ret = wait_for_atomic(host2guc_action_response(dev_priv, &status), 10);
  104.         if (status != GUC2HOST_STATUS_SUCCESS) {
  105.                 /*
  106.                  * Either the GuC explicitly returned an error (which
  107.                  * we convert to -EIO here) or no response at all was
  108.                  * received within the timeout limit (-ETIMEDOUT)
  109.                  */
  110.                 if (ret != -ETIMEDOUT)
  111.                         ret = -EIO;
  112.  
  113.                 DRM_ERROR("GUC: host2guc action 0x%X failed. ret=%d "
  114.                                 "status=0x%08X response=0x%08X\n",
  115.                                 data[0], ret, status,
  116.                                 I915_READ(SOFT_SCRATCH(15)));
  117.  
  118.                 dev_priv->guc.action_fail += 1;
  119.                 dev_priv->guc.action_err = ret;
  120.         }
  121.         dev_priv->guc.action_status = status;
  122.  
  123.         spin_unlock(&dev_priv->guc.host2guc_lock);
  124.         intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  125.  
  126.         return ret;
  127. }
  128.  
  129. /*
  130.  * Tell the GuC to allocate or deallocate a specific doorbell
  131.  */
  132.  
  133. static int host2guc_allocate_doorbell(struct intel_guc *guc,
  134.                                       struct i915_guc_client *client)
  135. {
  136.         u32 data[2];
  137.  
  138.         data[0] = HOST2GUC_ACTION_ALLOCATE_DOORBELL;
  139.         data[1] = client->ctx_index;
  140.  
  141.         return host2guc_action(guc, data, 2);
  142. }
  143.  
  144. static int host2guc_release_doorbell(struct intel_guc *guc,
  145.                                      struct i915_guc_client *client)
  146. {
  147.         u32 data[2];
  148.  
  149.         data[0] = HOST2GUC_ACTION_DEALLOCATE_DOORBELL;
  150.         data[1] = client->ctx_index;
  151.  
  152.         return host2guc_action(guc, data, 2);
  153. }
  154.  
  155. static int host2guc_sample_forcewake(struct intel_guc *guc,
  156.                                      struct i915_guc_client *client)
  157. {
  158.         struct drm_i915_private *dev_priv = guc_to_i915(guc);
  159.         struct drm_device *dev = dev_priv->dev;
  160.         u32 data[2];
  161.  
  162.         data[0] = HOST2GUC_ACTION_SAMPLE_FORCEWAKE;
  163.         /* WaRsDisableCoarsePowerGating:skl,bxt */
  164.         if (!intel_enable_rc6(dev_priv->dev) ||
  165.             (IS_BROXTON(dev) && (INTEL_REVID(dev) < BXT_REVID_B0)) ||
  166.             (IS_SKL_GT3(dev) && (INTEL_REVID(dev) <= SKL_REVID_E0)) ||
  167.             (IS_SKL_GT4(dev) && (INTEL_REVID(dev) <= SKL_REVID_E0)))
  168.                 data[1] = 0;
  169.         else
  170.                 /* bit 0 and 1 are for Render and Media domain separately */
  171.                 data[1] = GUC_FORCEWAKE_RENDER | GUC_FORCEWAKE_MEDIA;
  172.  
  173.         return host2guc_action(guc, data, ARRAY_SIZE(data));
  174. }
  175.  
  176. /*
  177.  * Initialise, update, or clear doorbell data shared with the GuC
  178.  *
  179.  * These functions modify shared data and so need access to the mapped
  180.  * client object which contains the page being used for the doorbell
  181.  */
  182.  
  183. static void guc_init_doorbell(struct intel_guc *guc,
  184.                               struct i915_guc_client *client)
  185. {
  186.         struct guc_doorbell_info *doorbell;
  187.         void *base;
  188.  
  189.         base = kmap_atomic(i915_gem_object_get_page(client->client_obj, 0));
  190.         doorbell = base + client->doorbell_offset;
  191.  
  192.         doorbell->db_status = 1;
  193.         doorbell->cookie = 0;
  194.  
  195.         kunmap_atomic(base);
  196. }
  197.  
  198. static int guc_ring_doorbell(struct i915_guc_client *gc)
  199. {
  200.         struct guc_process_desc *desc;
  201.         union guc_doorbell_qw db_cmp, db_exc, db_ret;
  202.         union guc_doorbell_qw *db;
  203.         void *base;
  204.         int attempt = 2, ret = -EAGAIN;
  205.  
  206.         base = kmap_atomic(i915_gem_object_get_page(gc->client_obj, 0));
  207.         desc = base + gc->proc_desc_offset;
  208.  
  209.         /* Update the tail so it is visible to GuC */
  210.         desc->tail = gc->wq_tail;
  211.  
  212.         /* current cookie */
  213.         db_cmp.db_status = GUC_DOORBELL_ENABLED;
  214.         db_cmp.cookie = gc->cookie;
  215.  
  216.         /* cookie to be updated */
  217.         db_exc.db_status = GUC_DOORBELL_ENABLED;
  218.         db_exc.cookie = gc->cookie + 1;
  219.         if (db_exc.cookie == 0)
  220.                 db_exc.cookie = 1;
  221.  
  222.         /* pointer of current doorbell cacheline */
  223.         db = base + gc->doorbell_offset;
  224.  
  225.         while (attempt--) {
  226.                 /* lets ring the doorbell */
  227.                 db_ret.value_qw = atomic64_cmpxchg((atomic64_t *)db,
  228.                         db_cmp.value_qw, db_exc.value_qw);
  229.  
  230.                 /* if the exchange was successfully executed */
  231.                 if (db_ret.value_qw == db_cmp.value_qw) {
  232.                         /* db was successfully rung */
  233.                         gc->cookie = db_exc.cookie;
  234.                         ret = 0;
  235.                         break;
  236.                 }
  237.  
  238.                 /* XXX: doorbell was lost and need to acquire it again */
  239.                 if (db_ret.db_status == GUC_DOORBELL_DISABLED)
  240.                         break;
  241.  
  242.                 DRM_ERROR("Cookie mismatch. Expected %d, returned %d\n",
  243.                           db_cmp.cookie, db_ret.cookie);
  244.  
  245.                 /* update the cookie to newly read cookie from GuC */
  246.                 db_cmp.cookie = db_ret.cookie;
  247.                 db_exc.cookie = db_ret.cookie + 1;
  248.                 if (db_exc.cookie == 0)
  249.                         db_exc.cookie = 1;
  250.         }
  251.  
  252.         kunmap_atomic(base);
  253.         return ret;
  254. }
  255.  
  256. static void guc_disable_doorbell(struct intel_guc *guc,
  257.                                  struct i915_guc_client *client)
  258. {
  259.         struct drm_i915_private *dev_priv = guc_to_i915(guc);
  260.         struct guc_doorbell_info *doorbell;
  261.         void *base;
  262.         int drbreg = GEN8_DRBREGL(client->doorbell_id);
  263.         int value;
  264.  
  265.         base = kmap_atomic(i915_gem_object_get_page(client->client_obj, 0));
  266.         doorbell = base + client->doorbell_offset;
  267.  
  268.         doorbell->db_status = 0;
  269.  
  270.         kunmap_atomic(base);
  271.  
  272.         I915_WRITE(drbreg, I915_READ(drbreg) & ~GEN8_DRB_VALID);
  273.  
  274.         value = I915_READ(drbreg);
  275.         WARN_ON((value & GEN8_DRB_VALID) != 0);
  276.  
  277.         I915_WRITE(GEN8_DRBREGU(client->doorbell_id), 0);
  278.         I915_WRITE(drbreg, 0);
  279.  
  280.         /* XXX: wait for any interrupts */
  281.         /* XXX: wait for workqueue to drain */
  282. }
  283.  
  284. /*
  285.  * Select, assign and relase doorbell cachelines
  286.  *
  287.  * These functions track which doorbell cachelines are in use.
  288.  * The data they manipulate is protected by the host2guc lock.
  289.  */
  290.  
  291. static uint32_t select_doorbell_cacheline(struct intel_guc *guc)
  292. {
  293.         const uint32_t cacheline_size = cache_line_size();
  294.         uint32_t offset;
  295.  
  296.         spin_lock(&guc->host2guc_lock);
  297.  
  298.         /* Doorbell uses a single cache line within a page */
  299.         offset = offset_in_page(guc->db_cacheline);
  300.  
  301.         /* Moving to next cache line to reduce contention */
  302.         guc->db_cacheline += cacheline_size;
  303.  
  304.         spin_unlock(&guc->host2guc_lock);
  305.  
  306.         DRM_DEBUG_DRIVER("selected doorbell cacheline 0x%x, next 0x%x, linesize %u\n",
  307.                         offset, guc->db_cacheline, cacheline_size);
  308.  
  309.         return offset;
  310. }
  311.  
  312. static uint16_t assign_doorbell(struct intel_guc *guc, uint32_t priority)
  313. {
  314.         /*
  315.          * The bitmap is split into two halves; the first half is used for
  316.          * normal priority contexts, the second half for high-priority ones.
  317.          * Note that logically higher priorities are numerically less than
  318.          * normal ones, so the test below means "is it high-priority?"
  319.          */
  320.         const bool hi_pri = (priority <= GUC_CTX_PRIORITY_HIGH);
  321.         const uint16_t half = GUC_MAX_DOORBELLS / 2;
  322.         const uint16_t start = hi_pri ? half : 0;
  323.         const uint16_t end = start + half;
  324.         uint16_t id;
  325.  
  326.         spin_lock(&guc->host2guc_lock);
  327.         id = find_next_zero_bit(guc->doorbell_bitmap, end, start);
  328.         if (id == end)
  329.                 id = GUC_INVALID_DOORBELL_ID;
  330.         else
  331.                 bitmap_set(guc->doorbell_bitmap, id, 1);
  332.         spin_unlock(&guc->host2guc_lock);
  333.  
  334.         DRM_DEBUG_DRIVER("assigned %s priority doorbell id 0x%x\n",
  335.                         hi_pri ? "high" : "normal", id);
  336.  
  337.         return id;
  338. }
  339.  
  340. static void release_doorbell(struct intel_guc *guc, uint16_t id)
  341. {
  342.         spin_lock(&guc->host2guc_lock);
  343.         bitmap_clear(guc->doorbell_bitmap, id, 1);
  344.         spin_unlock(&guc->host2guc_lock);
  345. }
  346.  
  347. /*
  348.  * Initialise the process descriptor shared with the GuC firmware.
  349.  */
  350. static void guc_init_proc_desc(struct intel_guc *guc,
  351.                                struct i915_guc_client *client)
  352. {
  353.         struct guc_process_desc *desc;
  354.         void *base;
  355.  
  356.         base = kmap_atomic(i915_gem_object_get_page(client->client_obj, 0));
  357.         desc = base + client->proc_desc_offset;
  358.  
  359.         memset(desc, 0, sizeof(*desc));
  360.  
  361.         /*
  362.          * XXX: pDoorbell and WQVBaseAddress are pointers in process address
  363.          * space for ring3 clients (set them as in mmap_ioctl) or kernel
  364.          * space for kernel clients (map on demand instead? May make debug
  365.          * easier to have it mapped).
  366.          */
  367.         desc->wq_base_addr = 0;
  368.         desc->db_base_addr = 0;
  369.  
  370.         desc->context_id = client->ctx_index;
  371.         desc->wq_size_bytes = client->wq_size;
  372.         desc->wq_status = WQ_STATUS_ACTIVE;
  373.         desc->priority = client->priority;
  374.  
  375.         kunmap_atomic(base);
  376. }
  377.  
  378. /*
  379.  * Initialise/clear the context descriptor shared with the GuC firmware.
  380.  *
  381.  * This descriptor tells the GuC where (in GGTT space) to find the important
  382.  * data structures relating to this client (doorbell, process descriptor,
  383.  * write queue, etc).
  384.  */
  385.  
  386. static void guc_init_ctx_desc(struct intel_guc *guc,
  387.                               struct i915_guc_client *client)
  388. {
  389.         struct intel_context *ctx = client->owner;
  390.         struct guc_context_desc desc;
  391.         struct sg_table *sg;
  392.         int i;
  393.  
  394.         memset(&desc, 0, sizeof(desc));
  395.  
  396.         desc.attribute = GUC_CTX_DESC_ATTR_ACTIVE | GUC_CTX_DESC_ATTR_KERNEL;
  397.         desc.context_id = client->ctx_index;
  398.         desc.priority = client->priority;
  399.         desc.db_id = client->doorbell_id;
  400.  
  401.         for (i = 0; i < I915_NUM_RINGS; i++) {
  402.                 struct guc_execlist_context *lrc = &desc.lrc[i];
  403.                 struct intel_ringbuffer *ringbuf = ctx->engine[i].ringbuf;
  404.                 struct intel_engine_cs *ring;
  405.                 struct drm_i915_gem_object *obj;
  406.                 uint64_t ctx_desc;
  407.  
  408.                 /* TODO: We have a design issue to be solved here. Only when we
  409.                  * receive the first batch, we know which engine is used by the
  410.                  * user. But here GuC expects the lrc and ring to be pinned. It
  411.                  * is not an issue for default context, which is the only one
  412.                  * for now who owns a GuC client. But for future owner of GuC
  413.                  * client, need to make sure lrc is pinned prior to enter here.
  414.                  */
  415.                 obj = ctx->engine[i].state;
  416.                 if (!obj)
  417.                         break;  /* XXX: continue? */
  418.  
  419.                 ring = ringbuf->ring;
  420.                 ctx_desc = intel_lr_context_descriptor(ctx, ring);
  421.                 lrc->context_desc = (u32)ctx_desc;
  422.  
  423.                 /* The state page is after PPHWSP */
  424.                 lrc->ring_lcra = i915_gem_obj_ggtt_offset(obj) +
  425.                                 LRC_STATE_PN * PAGE_SIZE;
  426.                 lrc->context_id = (client->ctx_index << GUC_ELC_CTXID_OFFSET) |
  427.                                 (ring->id << GUC_ELC_ENGINE_OFFSET);
  428.  
  429.                 obj = ringbuf->obj;
  430.  
  431.                 lrc->ring_begin = i915_gem_obj_ggtt_offset(obj);
  432.                 lrc->ring_end = lrc->ring_begin + obj->base.size - 1;
  433.                 lrc->ring_next_free_location = lrc->ring_begin;
  434.                 lrc->ring_current_tail_pointer_value = 0;
  435.  
  436.                 desc.engines_used |= (1 << ring->id);
  437.         }
  438.  
  439.         WARN_ON(desc.engines_used == 0);
  440.  
  441.         /*
  442.          * The CPU address is only needed at certain points, so kmap_atomic on
  443.          * demand instead of storing it in the ctx descriptor.
  444.          * XXX: May make debug easier to have it mapped
  445.          */
  446.         desc.db_trigger_cpu = 0;
  447.         desc.db_trigger_uk = client->doorbell_offset +
  448.                 i915_gem_obj_ggtt_offset(client->client_obj);
  449.         desc.db_trigger_phy = client->doorbell_offset +
  450.                 sg_dma_address(client->client_obj->pages->sgl);
  451.  
  452.         desc.process_desc = client->proc_desc_offset +
  453.                 i915_gem_obj_ggtt_offset(client->client_obj);
  454.  
  455.         desc.wq_addr = client->wq_offset +
  456.                 i915_gem_obj_ggtt_offset(client->client_obj);
  457.  
  458.         desc.wq_size = client->wq_size;
  459.  
  460.         /*
  461.          * XXX: Take LRCs from an existing intel_context if this is not an
  462.          * IsKMDCreatedContext client
  463.          */
  464.         desc.desc_private = (uintptr_t)client;
  465.  
  466.         /* Pool context is pinned already */
  467.         sg = guc->ctx_pool_obj->pages;
  468.         sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
  469.                              sizeof(desc) * client->ctx_index);
  470. }
  471.  
  472. static void guc_fini_ctx_desc(struct intel_guc *guc,
  473.                               struct i915_guc_client *client)
  474. {
  475.         struct guc_context_desc desc;
  476.         struct sg_table *sg;
  477.  
  478.         memset(&desc, 0, sizeof(desc));
  479.  
  480.         sg = guc->ctx_pool_obj->pages;
  481.         sg_pcopy_from_buffer(sg->sgl, sg->nents, &desc, sizeof(desc),
  482.                              sizeof(desc) * client->ctx_index);
  483. }
  484.  
  485. /* Get valid workqueue item and return it back to offset */
  486. static int guc_get_workqueue_space(struct i915_guc_client *gc, u32 *offset)
  487. {
  488.         struct guc_process_desc *desc;
  489.         void *base;
  490.         u32 size = sizeof(struct guc_wq_item);
  491.         int ret = 0, timeout_counter = 200;
  492.  
  493.         base = kmap_atomic(i915_gem_object_get_page(gc->client_obj, 0));
  494.         desc = base + gc->proc_desc_offset;
  495.  
  496.         while (timeout_counter-- > 0) {
  497.                 ret = wait_for_atomic(CIRC_SPACE(gc->wq_tail, desc->head,
  498.                                 gc->wq_size) >= size, 1);
  499.  
  500.                 if (!ret) {
  501.                         *offset = gc->wq_tail;
  502.  
  503.                         /* advance the tail for next workqueue item */
  504.                         gc->wq_tail += size;
  505.                         gc->wq_tail &= gc->wq_size - 1;
  506.  
  507.                         /* this will break the loop */
  508.                         timeout_counter = 0;
  509.                 }
  510.         };
  511.  
  512.         kunmap_atomic(base);
  513.  
  514.         return ret;
  515. }
  516.  
  517. static int guc_add_workqueue_item(struct i915_guc_client *gc,
  518.                                   struct drm_i915_gem_request *rq)
  519. {
  520.         enum intel_ring_id ring_id = rq->ring->id;
  521.         struct guc_wq_item *wqi;
  522.         void *base;
  523.         u32 tail, wq_len, wq_off = 0;
  524.         int ret;
  525.  
  526.         ret = guc_get_workqueue_space(gc, &wq_off);
  527.         if (ret)
  528.                 return ret;
  529.  
  530.         /* For now workqueue item is 4 DWs; workqueue buffer is 2 pages. So we
  531.          * should not have the case where structure wqi is across page, neither
  532.          * wrapped to the beginning. This simplifies the implementation below.
  533.          *
  534.          * XXX: if not the case, we need save data to a temp wqi and copy it to
  535.          * workqueue buffer dw by dw.
  536.          */
  537.         WARN_ON(sizeof(struct guc_wq_item) != 16);
  538.         WARN_ON(wq_off & 3);
  539.  
  540.         /* wq starts from the page after doorbell / process_desc */
  541.         base = kmap_atomic(i915_gem_object_get_page(gc->client_obj,
  542.                         (wq_off + GUC_DB_SIZE) >> PAGE_SHIFT));
  543.         wq_off &= PAGE_SIZE - 1;
  544.         wqi = (struct guc_wq_item *)((char *)base + wq_off);
  545.  
  546.         /* len does not include the header */
  547.         wq_len = sizeof(struct guc_wq_item) / sizeof(u32) - 1;
  548.         wqi->header = WQ_TYPE_INORDER |
  549.                         (wq_len << WQ_LEN_SHIFT) |
  550.                         (ring_id << WQ_TARGET_SHIFT) |
  551.                         WQ_NO_WCFLUSH_WAIT;
  552.  
  553.         /* The GuC wants only the low-order word of the context descriptor */
  554.         wqi->context_desc = (u32)intel_lr_context_descriptor(rq->ctx, rq->ring);
  555.  
  556.         /* The GuC firmware wants the tail index in QWords, not bytes */
  557.         tail = rq->ringbuf->tail >> 3;
  558.         wqi->ring_tail = tail << WQ_RING_TAIL_SHIFT;
  559.         wqi->fence_id = 0; /*XXX: what fence to be here */
  560.  
  561.         kunmap_atomic(base);
  562.  
  563.         return 0;
  564. }
  565.  
  566. #define CTX_RING_BUFFER_START           0x08
  567.  
  568. /* Update the ringbuffer pointer in a saved context image */
  569. static void lr_context_update(struct drm_i915_gem_request *rq)
  570. {
  571.         enum intel_ring_id ring_id = rq->ring->id;
  572.         struct drm_i915_gem_object *ctx_obj = rq->ctx->engine[ring_id].state;
  573.         struct drm_i915_gem_object *rb_obj = rq->ringbuf->obj;
  574.         struct page *page;
  575.         uint32_t *reg_state;
  576.  
  577.         BUG_ON(!ctx_obj);
  578.         WARN_ON(!i915_gem_obj_is_pinned(ctx_obj));
  579.         WARN_ON(!i915_gem_obj_is_pinned(rb_obj));
  580.  
  581.         page = i915_gem_object_get_page(ctx_obj, LRC_STATE_PN);
  582.         reg_state = kmap_atomic(page);
  583.  
  584.         reg_state[CTX_RING_BUFFER_START+1] = i915_gem_obj_ggtt_offset(rb_obj);
  585.  
  586.         kunmap_atomic(reg_state);
  587. }
  588.  
  589. /**
  590.  * i915_guc_submit() - Submit commands through GuC
  591.  * @client:     the guc client where commands will go through
  592.  * @ctx:        LRC where commands come from
  593.  * @ring:       HW engine that will excute the commands
  594.  *
  595.  * Return:      0 if succeed
  596.  */
  597. int i915_guc_submit(struct i915_guc_client *client,
  598.                     struct drm_i915_gem_request *rq)
  599. {
  600.         struct intel_guc *guc = client->guc;
  601.         enum intel_ring_id ring_id = rq->ring->id;
  602.         unsigned long flags;
  603.         int q_ret, b_ret;
  604.  
  605.         /* Need this because of the deferred pin ctx and ring */
  606.         /* Shall we move this right after ring is pinned? */
  607.         lr_context_update(rq);
  608.  
  609.         spin_lock_irqsave(&client->wq_lock, flags);
  610.  
  611.         q_ret = guc_add_workqueue_item(client, rq);
  612.         if (q_ret == 0)
  613.                 b_ret = guc_ring_doorbell(client);
  614.  
  615.         client->submissions[ring_id] += 1;
  616.         if (q_ret) {
  617.                 client->q_fail += 1;
  618.                 client->retcode = q_ret;
  619.         } else if (b_ret) {
  620.                 client->b_fail += 1;
  621.                 client->retcode = q_ret = b_ret;
  622.         } else {
  623.                 client->retcode = 0;
  624.         }
  625.         spin_unlock_irqrestore(&client->wq_lock, flags);
  626.  
  627.         spin_lock(&guc->host2guc_lock);
  628.         guc->submissions[ring_id] += 1;
  629.         guc->last_seqno[ring_id] = rq->seqno;
  630.         spin_unlock(&guc->host2guc_lock);
  631.  
  632.         return q_ret;
  633. }
  634.  
  635. /*
  636.  * Everything below here is concerned with setup & teardown, and is
  637.  * therefore not part of the somewhat time-critical batch-submission
  638.  * path of i915_guc_submit() above.
  639.  */
  640.  
  641. /**
  642.  * gem_allocate_guc_obj() - Allocate gem object for GuC usage
  643.  * @dev:        drm device
  644.  * @size:       size of object
  645.  *
  646.  * This is a wrapper to create a gem obj. In order to use it inside GuC, the
  647.  * object needs to be pinned lifetime. Also we must pin it to gtt space other
  648.  * than [0, GUC_WOPCM_TOP) because this range is reserved inside GuC.
  649.  *
  650.  * Return:      A drm_i915_gem_object if successful, otherwise NULL.
  651.  */
  652. static struct drm_i915_gem_object *gem_allocate_guc_obj(struct drm_device *dev,
  653.                                                         u32 size)
  654. {
  655.         struct drm_i915_private *dev_priv = dev->dev_private;
  656.         struct drm_i915_gem_object *obj;
  657.  
  658.         obj = i915_gem_alloc_object(dev, size);
  659.         if (!obj)
  660.                 return NULL;
  661.  
  662.         if (i915_gem_object_get_pages(obj)) {
  663.                 drm_gem_object_unreference(&obj->base);
  664.                 return NULL;
  665.         }
  666.  
  667.         if (i915_gem_obj_ggtt_pin(obj, PAGE_SIZE,
  668.                         PIN_OFFSET_BIAS | GUC_WOPCM_TOP)) {
  669.                 drm_gem_object_unreference(&obj->base);
  670.                 return NULL;
  671.         }
  672.  
  673.         /* Invalidate GuC TLB to let GuC take the latest updates to GTT. */
  674.         I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE);
  675.  
  676.         return obj;
  677. }
  678.  
  679. /**
  680.  * gem_release_guc_obj() - Release gem object allocated for GuC usage
  681.  * @obj:        gem obj to be released
  682.   */
  683. static void gem_release_guc_obj(struct drm_i915_gem_object *obj)
  684. {
  685.         if (!obj)
  686.                 return;
  687.  
  688.         if (i915_gem_obj_is_pinned(obj))
  689.                 i915_gem_object_ggtt_unpin(obj);
  690.  
  691.         drm_gem_object_unreference(&obj->base);
  692. }
  693.  
  694. static void guc_client_free(struct drm_device *dev,
  695.                             struct i915_guc_client *client)
  696. {
  697.         struct drm_i915_private *dev_priv = dev->dev_private;
  698.         struct intel_guc *guc = &dev_priv->guc;
  699.  
  700.         if (!client)
  701.                 return;
  702.  
  703.         if (client->doorbell_id != GUC_INVALID_DOORBELL_ID) {
  704.                 /*
  705.                  * First disable the doorbell, then tell the GuC we've
  706.                  * finished with it, finally deallocate it in our bitmap
  707.                  */
  708.                 guc_disable_doorbell(guc, client);
  709.                 host2guc_release_doorbell(guc, client);
  710.                 release_doorbell(guc, client->doorbell_id);
  711.         }
  712.  
  713.         /*
  714.          * XXX: wait for any outstanding submissions before freeing memory.
  715.          * Be sure to drop any locks
  716.          */
  717.  
  718.         gem_release_guc_obj(client->client_obj);
  719.  
  720.         if (client->ctx_index != GUC_INVALID_CTX_ID) {
  721.                 guc_fini_ctx_desc(guc, client);
  722.                 ida_simple_remove(&guc->ctx_ids, client->ctx_index);
  723.         }
  724.  
  725.         kfree(client);
  726. }
  727.  
  728. /**
  729.  * guc_client_alloc() - Allocate an i915_guc_client
  730.  * @dev:        drm device
  731.  * @priority:   four levels priority _CRITICAL, _HIGH, _NORMAL and _LOW
  732.  *              The kernel client to replace ExecList submission is created with
  733.  *              NORMAL priority. Priority of a client for scheduler can be HIGH,
  734.  *              while a preemption context can use CRITICAL.
  735.  * @ctx         the context to own the client (we use the default render context)
  736.  *
  737.  * Return:      An i915_guc_client object if success.
  738.  */
  739. static struct i915_guc_client *guc_client_alloc(struct drm_device *dev,
  740.                                                 uint32_t priority,
  741.                                                 struct intel_context *ctx)
  742. {
  743.         struct i915_guc_client *client;
  744.         struct drm_i915_private *dev_priv = dev->dev_private;
  745.         struct intel_guc *guc = &dev_priv->guc;
  746.         struct drm_i915_gem_object *obj;
  747.  
  748.         client = kzalloc(sizeof(*client), GFP_KERNEL);
  749.         if (!client)
  750.                 return NULL;
  751.  
  752.         client->doorbell_id = GUC_INVALID_DOORBELL_ID;
  753.         client->priority = priority;
  754.         client->owner = ctx;
  755.         client->guc = guc;
  756.  
  757.         client->ctx_index = (uint32_t)ida_simple_get(&guc->ctx_ids, 0,
  758.                         GUC_MAX_GPU_CONTEXTS, GFP_KERNEL);
  759.         if (client->ctx_index >= GUC_MAX_GPU_CONTEXTS) {
  760.                 client->ctx_index = GUC_INVALID_CTX_ID;
  761.                 goto err;
  762.         }
  763.  
  764.         /* The first page is doorbell/proc_desc. Two followed pages are wq. */
  765.         obj = gem_allocate_guc_obj(dev, GUC_DB_SIZE + GUC_WQ_SIZE);
  766.         if (!obj)
  767.                 goto err;
  768.  
  769.         client->client_obj = obj;
  770.         client->wq_offset = GUC_DB_SIZE;
  771.         client->wq_size = GUC_WQ_SIZE;
  772.         spin_lock_init(&client->wq_lock);
  773.  
  774.         client->doorbell_offset = select_doorbell_cacheline(guc);
  775.  
  776.         /*
  777.          * Since the doorbell only requires a single cacheline, we can save
  778.          * space by putting the application process descriptor in the same
  779.          * page. Use the half of the page that doesn't include the doorbell.
  780.          */
  781.         if (client->doorbell_offset >= (GUC_DB_SIZE / 2))
  782.                 client->proc_desc_offset = 0;
  783.         else
  784.                 client->proc_desc_offset = (GUC_DB_SIZE / 2);
  785.  
  786.         client->doorbell_id = assign_doorbell(guc, client->priority);
  787.         if (client->doorbell_id == GUC_INVALID_DOORBELL_ID)
  788.                 /* XXX: evict a doorbell instead */
  789.                 goto err;
  790.  
  791.         guc_init_proc_desc(guc, client);
  792.         guc_init_ctx_desc(guc, client);
  793.         guc_init_doorbell(guc, client);
  794.  
  795.         /* XXX: Any cache flushes needed? General domain mgmt calls? */
  796.  
  797.         if (host2guc_allocate_doorbell(guc, client))
  798.                 goto err;
  799.  
  800.         DRM_DEBUG_DRIVER("new priority %u client %p: ctx_index %u db_id %u\n",
  801.                 priority, client, client->ctx_index, client->doorbell_id);
  802.  
  803.         return client;
  804.  
  805. err:
  806.         DRM_ERROR("FAILED to create priority %u GuC client!\n", priority);
  807.  
  808.         guc_client_free(dev, client);
  809.         return NULL;
  810. }
  811.  
  812. static void guc_create_log(struct intel_guc *guc)
  813. {
  814.         struct drm_i915_private *dev_priv = guc_to_i915(guc);
  815.         struct drm_i915_gem_object *obj;
  816.         unsigned long offset;
  817.         uint32_t size, flags;
  818.  
  819.         if (i915.guc_log_level < GUC_LOG_VERBOSITY_MIN)
  820.                 return;
  821.  
  822.         if (i915.guc_log_level > GUC_LOG_VERBOSITY_MAX)
  823.                 i915.guc_log_level = GUC_LOG_VERBOSITY_MAX;
  824.  
  825.         /* The first page is to save log buffer state. Allocate one
  826.          * extra page for others in case for overlap */
  827.         size = (1 + GUC_LOG_DPC_PAGES + 1 +
  828.                 GUC_LOG_ISR_PAGES + 1 +
  829.                 GUC_LOG_CRASH_PAGES + 1) << PAGE_SHIFT;
  830.  
  831.         obj = guc->log_obj;
  832.         if (!obj) {
  833.                 obj = gem_allocate_guc_obj(dev_priv->dev, size);
  834.                 if (!obj) {
  835.                         /* logging will be off */
  836.                         i915.guc_log_level = -1;
  837.                         return;
  838.                 }
  839.  
  840.                 guc->log_obj = obj;
  841.         }
  842.  
  843.         /* each allocated unit is a page */
  844.         flags = GUC_LOG_VALID | GUC_LOG_NOTIFY_ON_HALF_FULL |
  845.                 (GUC_LOG_DPC_PAGES << GUC_LOG_DPC_SHIFT) |
  846.                 (GUC_LOG_ISR_PAGES << GUC_LOG_ISR_SHIFT) |
  847.                 (GUC_LOG_CRASH_PAGES << GUC_LOG_CRASH_SHIFT);
  848.  
  849.         offset = i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT; /* in pages */
  850.         guc->log_flags = (offset << GUC_LOG_BUF_ADDR_SHIFT) | flags;
  851. }
  852.  
  853. /*
  854.  * Set up the memory resources to be shared with the GuC.  At this point,
  855.  * we require just one object that can be mapped through the GGTT.
  856.  */
  857. int i915_guc_submission_init(struct drm_device *dev)
  858. {
  859.         struct drm_i915_private *dev_priv = dev->dev_private;
  860.         const size_t ctxsize = sizeof(struct guc_context_desc);
  861.         const size_t poolsize = GUC_MAX_GPU_CONTEXTS * ctxsize;
  862.         const size_t gemsize = round_up(poolsize, PAGE_SIZE);
  863.         struct intel_guc *guc = &dev_priv->guc;
  864.  
  865.         if (!i915.enable_guc_submission)
  866.                 return 0; /* not enabled  */
  867.  
  868.         if (guc->ctx_pool_obj)
  869.                 return 0; /* already allocated */
  870.  
  871.         guc->ctx_pool_obj = gem_allocate_guc_obj(dev_priv->dev, gemsize);
  872.         if (!guc->ctx_pool_obj)
  873.                 return -ENOMEM;
  874.  
  875.         spin_lock_init(&dev_priv->guc.host2guc_lock);
  876.  
  877.         ida_init(&guc->ctx_ids);
  878.  
  879.         guc_create_log(guc);
  880.  
  881.         return 0;
  882. }
  883.  
  884. int i915_guc_submission_enable(struct drm_device *dev)
  885. {
  886.         struct drm_i915_private *dev_priv = dev->dev_private;
  887.         struct intel_guc *guc = &dev_priv->guc;
  888.         struct intel_context *ctx = dev_priv->ring[RCS].default_context;
  889.         struct i915_guc_client *client;
  890.  
  891.         /* client for execbuf submission */
  892.         client = guc_client_alloc(dev, GUC_CTX_PRIORITY_KMD_NORMAL, ctx);
  893.         if (!client) {
  894.                 DRM_ERROR("Failed to create execbuf guc_client\n");
  895.                 return -ENOMEM;
  896.         }
  897.  
  898.         guc->execbuf_client = client;
  899.  
  900.         host2guc_sample_forcewake(guc, client);
  901.  
  902.         return 0;
  903. }
  904.  
  905. void i915_guc_submission_disable(struct drm_device *dev)
  906. {
  907.         struct drm_i915_private *dev_priv = dev->dev_private;
  908.         struct intel_guc *guc = &dev_priv->guc;
  909.  
  910.         guc_client_free(dev, guc->execbuf_client);
  911.         guc->execbuf_client = NULL;
  912. }
  913.  
  914. void i915_guc_submission_fini(struct drm_device *dev)
  915. {
  916.         struct drm_i915_private *dev_priv = dev->dev_private;
  917.         struct intel_guc *guc = &dev_priv->guc;
  918.  
  919.         gem_release_guc_obj(dev_priv->guc.log_obj);
  920.         guc->log_obj = NULL;
  921.  
  922.         if (guc->ctx_pool_obj)
  923.                 ida_destroy(&guc->ctx_ids);
  924.         gem_release_guc_obj(guc->ctx_pool_obj);
  925.         guc->ctx_pool_obj = NULL;
  926. }
  927.  
  928. /**
  929.  * intel_guc_suspend() - notify GuC entering suspend state
  930.  * @dev:        drm device
  931.  */
  932. int intel_guc_suspend(struct drm_device *dev)
  933. {
  934.         struct drm_i915_private *dev_priv = dev->dev_private;
  935.         struct intel_guc *guc = &dev_priv->guc;
  936.         struct intel_context *ctx;
  937.         u32 data[3];
  938.  
  939.         if (!i915.enable_guc_submission)
  940.                 return 0;
  941.  
  942.         ctx = dev_priv->ring[RCS].default_context;
  943.  
  944.         data[0] = HOST2GUC_ACTION_ENTER_S_STATE;
  945.         /* any value greater than GUC_POWER_D0 */
  946.         data[1] = GUC_POWER_D1;
  947.         /* first page is shared data with GuC */
  948.         data[2] = i915_gem_obj_ggtt_offset(ctx->engine[RCS].state);
  949.  
  950.         return host2guc_action(guc, data, ARRAY_SIZE(data));
  951. }
  952.  
  953.  
  954. /**
  955.  * intel_guc_resume() - notify GuC resuming from suspend state
  956.  * @dev:        drm device
  957.  */
  958. int intel_guc_resume(struct drm_device *dev)
  959. {
  960.         struct drm_i915_private *dev_priv = dev->dev_private;
  961.         struct intel_guc *guc = &dev_priv->guc;
  962.         struct intel_context *ctx;
  963.         u32 data[3];
  964.  
  965.         if (!i915.enable_guc_submission)
  966.                 return 0;
  967.  
  968.         ctx = dev_priv->ring[RCS].default_context;
  969.  
  970.         data[0] = HOST2GUC_ACTION_EXIT_S_STATE;
  971.         data[1] = GUC_POWER_D0;
  972.         /* first page is shared data with GuC */
  973.         data[2] = i915_gem_obj_ggtt_offset(ctx->engine[RCS].state);
  974.  
  975.         return host2guc_action(guc, data, ARRAY_SIZE(data));
  976. }
  977.