Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 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 "i915_drv.h" |
| 27 | #include "intel_guc.h" |
| 28 | |
| 29 | /** |
| 30 | * gem_allocate_guc_obj() - Allocate gem object for GuC usage |
| 31 | * @dev: drm device |
| 32 | * @size: size of object |
| 33 | * |
| 34 | * This is a wrapper to create a gem obj. In order to use it inside GuC, the |
| 35 | * object needs to be pinned lifetime. Also we must pin it to gtt space other |
| 36 | * than [0, GUC_WOPCM_TOP) because this range is reserved inside GuC. |
| 37 | * |
| 38 | * Return: A drm_i915_gem_object if successful, otherwise NULL. |
| 39 | */ |
| 40 | static struct drm_i915_gem_object *gem_allocate_guc_obj(struct drm_device *dev, |
| 41 | u32 size) |
| 42 | { |
| 43 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 44 | struct drm_i915_gem_object *obj; |
| 45 | |
| 46 | obj = i915_gem_alloc_object(dev, size); |
| 47 | if (!obj) |
| 48 | return NULL; |
| 49 | |
| 50 | if (i915_gem_object_get_pages(obj)) { |
| 51 | drm_gem_object_unreference(&obj->base); |
| 52 | return NULL; |
| 53 | } |
| 54 | |
| 55 | if (i915_gem_obj_ggtt_pin(obj, PAGE_SIZE, |
| 56 | PIN_OFFSET_BIAS | GUC_WOPCM_TOP)) { |
| 57 | drm_gem_object_unreference(&obj->base); |
| 58 | return NULL; |
| 59 | } |
| 60 | |
| 61 | /* Invalidate GuC TLB to let GuC take the latest updates to GTT. */ |
| 62 | I915_WRITE(GEN8_GTCR, GEN8_GTCR_INVALIDATE); |
| 63 | |
| 64 | return obj; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * gem_release_guc_obj() - Release gem object allocated for GuC usage |
| 69 | * @obj: gem obj to be released |
| 70 | */ |
| 71 | static void gem_release_guc_obj(struct drm_i915_gem_object *obj) |
| 72 | { |
| 73 | if (!obj) |
| 74 | return; |
| 75 | |
| 76 | if (i915_gem_obj_is_pinned(obj)) |
| 77 | i915_gem_object_ggtt_unpin(obj); |
| 78 | |
| 79 | drm_gem_object_unreference(&obj->base); |
| 80 | } |
| 81 | |
Alex Dai | 4c7e77f | 2015-08-12 15:43:40 +0100 | [diff] [blame^] | 82 | static void guc_create_log(struct intel_guc *guc) |
| 83 | { |
| 84 | struct drm_i915_private *dev_priv = guc_to_i915(guc); |
| 85 | struct drm_i915_gem_object *obj; |
| 86 | unsigned long offset; |
| 87 | uint32_t size, flags; |
| 88 | |
| 89 | if (i915.guc_log_level < GUC_LOG_VERBOSITY_MIN) |
| 90 | return; |
| 91 | |
| 92 | if (i915.guc_log_level > GUC_LOG_VERBOSITY_MAX) |
| 93 | i915.guc_log_level = GUC_LOG_VERBOSITY_MAX; |
| 94 | |
| 95 | /* The first page is to save log buffer state. Allocate one |
| 96 | * extra page for others in case for overlap */ |
| 97 | size = (1 + GUC_LOG_DPC_PAGES + 1 + |
| 98 | GUC_LOG_ISR_PAGES + 1 + |
| 99 | GUC_LOG_CRASH_PAGES + 1) << PAGE_SHIFT; |
| 100 | |
| 101 | obj = guc->log_obj; |
| 102 | if (!obj) { |
| 103 | obj = gem_allocate_guc_obj(dev_priv->dev, size); |
| 104 | if (!obj) { |
| 105 | /* logging will be off */ |
| 106 | i915.guc_log_level = -1; |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | guc->log_obj = obj; |
| 111 | } |
| 112 | |
| 113 | /* each allocated unit is a page */ |
| 114 | flags = GUC_LOG_VALID | GUC_LOG_NOTIFY_ON_HALF_FULL | |
| 115 | (GUC_LOG_DPC_PAGES << GUC_LOG_DPC_SHIFT) | |
| 116 | (GUC_LOG_ISR_PAGES << GUC_LOG_ISR_SHIFT) | |
| 117 | (GUC_LOG_CRASH_PAGES << GUC_LOG_CRASH_SHIFT); |
| 118 | |
| 119 | offset = i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT; /* in pages */ |
| 120 | guc->log_flags = (offset << GUC_LOG_BUF_ADDR_SHIFT) | flags; |
| 121 | } |
| 122 | |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 123 | /* |
| 124 | * Set up the memory resources to be shared with the GuC. At this point, |
| 125 | * we require just one object that can be mapped through the GGTT. |
| 126 | */ |
| 127 | int i915_guc_submission_init(struct drm_device *dev) |
| 128 | { |
| 129 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 130 | const size_t ctxsize = sizeof(struct guc_context_desc); |
| 131 | const size_t poolsize = GUC_MAX_GPU_CONTEXTS * ctxsize; |
| 132 | const size_t gemsize = round_up(poolsize, PAGE_SIZE); |
| 133 | struct intel_guc *guc = &dev_priv->guc; |
| 134 | |
| 135 | if (!i915.enable_guc_submission) |
| 136 | return 0; /* not enabled */ |
| 137 | |
| 138 | if (guc->ctx_pool_obj) |
| 139 | return 0; /* already allocated */ |
| 140 | |
| 141 | guc->ctx_pool_obj = gem_allocate_guc_obj(dev_priv->dev, gemsize); |
| 142 | if (!guc->ctx_pool_obj) |
| 143 | return -ENOMEM; |
| 144 | |
| 145 | ida_init(&guc->ctx_ids); |
| 146 | |
Alex Dai | 4c7e77f | 2015-08-12 15:43:40 +0100 | [diff] [blame^] | 147 | guc_create_log(guc); |
| 148 | |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | void i915_guc_submission_fini(struct drm_device *dev) |
| 153 | { |
| 154 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 155 | struct intel_guc *guc = &dev_priv->guc; |
| 156 | |
Alex Dai | 4c7e77f | 2015-08-12 15:43:40 +0100 | [diff] [blame^] | 157 | gem_release_guc_obj(dev_priv->guc.log_obj); |
| 158 | guc->log_obj = NULL; |
| 159 | |
Alex Dai | bac427f | 2015-08-12 15:43:39 +0100 | [diff] [blame] | 160 | if (guc->ctx_pool_obj) |
| 161 | ida_destroy(&guc->ctx_ids); |
| 162 | gem_release_guc_obj(guc->ctx_pool_obj); |
| 163 | guc->ctx_pool_obj = NULL; |
| 164 | } |