drm/i915: stop using seqcount for fence pruning
[linux-2.6-block.git] / drivers / gpu / drm / i915 / intel_guc_ads.c
1 /*
2  * Copyright © 2014-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 "intel_guc_ads.h"
26 #include "intel_uc.h"
27 #include "i915_drv.h"
28
29 /*
30  * The Additional Data Struct (ADS) has pointers for different buffers used by
31  * the GuC. One single gem object contains the ADS struct itself (guc_ads), the
32  * scheduling policies (guc_policies), a structure describing a collection of
33  * register sets (guc_mmio_reg_state) and some extra pages for the GuC to save
34  * its internal state for sleep.
35  */
36
37 static void guc_policy_init(struct guc_policy *policy)
38 {
39         policy->execution_quantum = POLICY_DEFAULT_EXECUTION_QUANTUM_US;
40         policy->preemption_time = POLICY_DEFAULT_PREEMPTION_TIME_US;
41         policy->fault_time = POLICY_DEFAULT_FAULT_TIME_US;
42         policy->policy_flags = 0;
43 }
44
45 static void guc_policies_init(struct guc_policies *policies)
46 {
47         struct guc_policy *policy;
48         u32 p, i;
49
50         policies->dpc_promote_time = POLICY_DEFAULT_DPC_PROMOTE_TIME_US;
51         policies->max_num_work_items = POLICY_MAX_NUM_WI;
52
53         for (p = 0; p < GUC_CLIENT_PRIORITY_NUM; p++) {
54                 for (i = 0; i < GUC_MAX_ENGINE_CLASSES; i++) {
55                         policy = &policies->policy[p][i];
56
57                         guc_policy_init(policy);
58                 }
59         }
60
61         policies->is_valid = 1;
62 }
63
64 static void guc_ct_pool_entries_init(struct guc_ct_pool_entry *pool, u32 num)
65 {
66         memset(pool, 0, num * sizeof(*pool));
67 }
68
69 /*
70  * The first 80 dwords of the register state context, containing the
71  * execlists and ppgtt registers.
72  */
73 #define LR_HW_CONTEXT_SIZE      (80 * sizeof(u32))
74
75 /* The ads obj includes the struct itself and buffers passed to GuC */
76 struct __guc_ads_blob {
77         struct guc_ads ads;
78         struct guc_policies policies;
79         struct guc_mmio_reg_state reg_state;
80         struct guc_gt_system_info system_info;
81         struct guc_clients_info clients_info;
82         struct guc_ct_pool_entry ct_pool[GUC_CT_POOL_SIZE];
83         u8 reg_state_buffer[GUC_S3_SAVE_SPACE_PAGES * PAGE_SIZE];
84 } __packed;
85
86 static int __guc_ads_init(struct intel_guc *guc)
87 {
88         struct drm_i915_private *dev_priv = guc_to_i915(guc);
89         struct __guc_ads_blob *blob;
90         const u32 skipped_size = LRC_PPHWSP_SZ * PAGE_SIZE + LR_HW_CONTEXT_SIZE;
91         u32 base;
92         u8 engine_class;
93
94         blob = i915_gem_object_pin_map(guc->ads_vma->obj, I915_MAP_WB);
95         if (IS_ERR(blob))
96                 return PTR_ERR(blob);
97
98         /* GuC scheduling policies */
99         guc_policies_init(&blob->policies);
100
101         /*
102          * GuC expects a per-engine-class context image and size
103          * (minus hwsp and ring context). The context image will be
104          * used to reinitialize engines after a reset. It must exist
105          * and be pinned in the GGTT, so that the address won't change after
106          * we have told GuC where to find it. The context size will be used
107          * to validate that the LRC base + size fall within allowed GGTT.
108          */
109         for (engine_class = 0; engine_class <= MAX_ENGINE_CLASS; ++engine_class) {
110                 if (engine_class == OTHER_CLASS)
111                         continue;
112                 /*
113                  * TODO: Set context pointer to default state to allow
114                  * GuC to re-init guilty contexts after internal reset.
115                  */
116                 blob->ads.golden_context_lrca[engine_class] = 0;
117                 blob->ads.eng_state_size[engine_class] =
118                         intel_engine_context_size(dev_priv, engine_class) -
119                         skipped_size;
120         }
121
122         /* System info */
123         blob->system_info.slice_enabled = hweight8(RUNTIME_INFO(dev_priv)->sseu.slice_mask);
124         blob->system_info.rcs_enabled = 1;
125         blob->system_info.bcs_enabled = 1;
126
127         blob->system_info.vdbox_enable_mask = VDBOX_MASK(dev_priv);
128         blob->system_info.vebox_enable_mask = VEBOX_MASK(dev_priv);
129         blob->system_info.vdbox_sfc_support_mask = RUNTIME_INFO(dev_priv)->vdbox_sfc_access;
130
131         base = intel_guc_ggtt_offset(guc, guc->ads_vma);
132
133         /* Clients info  */
134         guc_ct_pool_entries_init(blob->ct_pool, ARRAY_SIZE(blob->ct_pool));
135
136         blob->clients_info.clients_num = 1;
137         blob->clients_info.ct_pool_addr = base + ptr_offset(blob, ct_pool);
138         blob->clients_info.ct_pool_count = ARRAY_SIZE(blob->ct_pool);
139
140         /* ADS */
141         blob->ads.scheduler_policies = base + ptr_offset(blob, policies);
142         blob->ads.reg_state_buffer = base + ptr_offset(blob, reg_state_buffer);
143         blob->ads.reg_state_addr = base + ptr_offset(blob, reg_state);
144         blob->ads.gt_system_info = base + ptr_offset(blob, system_info);
145         blob->ads.clients_info = base + ptr_offset(blob, clients_info);
146
147         i915_gem_object_unpin_map(guc->ads_vma->obj);
148
149         return 0;
150 }
151
152 /**
153  * intel_guc_ads_create() - allocates and initializes GuC ADS.
154  * @guc: intel_guc struct
155  *
156  * GuC needs memory block (Additional Data Struct), where it will store
157  * some data. Allocate and initialize such memory block for GuC use.
158  */
159 int intel_guc_ads_create(struct intel_guc *guc)
160 {
161         const u32 size = PAGE_ALIGN(sizeof(struct __guc_ads_blob));
162         struct i915_vma *vma;
163         int ret;
164
165         GEM_BUG_ON(guc->ads_vma);
166
167         vma = intel_guc_allocate_vma(guc, size);
168         if (IS_ERR(vma))
169                 return PTR_ERR(vma);
170
171         guc->ads_vma = vma;
172
173         ret = __guc_ads_init(guc);
174         if (ret)
175                 goto err_vma;
176
177         return 0;
178
179 err_vma:
180         i915_vma_unpin_and_release(&guc->ads_vma, 0);
181         return ret;
182 }
183
184 void intel_guc_ads_destroy(struct intel_guc *guc)
185 {
186         i915_vma_unpin_and_release(&guc->ads_vma, 0);
187 }
188
189 /**
190  * intel_guc_ads_reset() - prepares GuC Additional Data Struct for reuse
191  * @guc: intel_guc struct
192  *
193  * GuC stores some data in ADS, which might be stale after a reset.
194  * Reinitialize whole ADS in case any part of it was corrupted during
195  * previous GuC run.
196  */
197 void intel_guc_ads_reset(struct intel_guc *guc)
198 {
199         if (!guc->ads_vma)
200                 return;
201         __guc_ads_init(guc);
202 }