2 * Copyright © 2016-2017 Intel Corporation
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:
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
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
25 #include "intel_guc_ct.h"
27 enum { CTB_SEND
= 0, CTB_RECV
= 1 };
29 enum { CTB_OWNER_HOST
= 0 };
31 void intel_guc_ct_init_early(struct intel_guc_ct
*ct
)
33 /* we're using static channel owners */
34 ct
->host_channel
.owner
= CTB_OWNER_HOST
;
37 static inline const char *guc_ct_buffer_type_to_str(u32 type
)
40 case INTEL_GUC_CT_BUFFER_TYPE_SEND
:
42 case INTEL_GUC_CT_BUFFER_TYPE_RECV
:
49 static void guc_ct_buffer_desc_init(struct guc_ct_buffer_desc
*desc
,
50 u32 cmds_addr
, u32 size
, u32 owner
)
52 DRM_DEBUG_DRIVER("CT: desc %p init addr=%#x size=%u owner=%u\n",
53 desc
, cmds_addr
, size
, owner
);
54 memset(desc
, 0, sizeof(*desc
));
55 desc
->addr
= cmds_addr
;
60 static void guc_ct_buffer_desc_reset(struct guc_ct_buffer_desc
*desc
)
62 DRM_DEBUG_DRIVER("CT: desc %p reset head=%u tail=%u\n",
63 desc
, desc
->head
, desc
->tail
);
66 desc
->is_in_error
= 0;
69 static int guc_action_register_ct_buffer(struct intel_guc
*guc
,
74 INTEL_GUC_ACTION_REGISTER_COMMAND_TRANSPORT_BUFFER
,
76 sizeof(struct guc_ct_buffer_desc
),
81 /* Can't use generic send(), CT registration must go over MMIO */
82 err
= intel_guc_send_mmio(guc
, action
, ARRAY_SIZE(action
));
84 DRM_ERROR("CT: register %s buffer failed; err=%d\n",
85 guc_ct_buffer_type_to_str(type
), err
);
89 static int guc_action_deregister_ct_buffer(struct intel_guc
*guc
,
94 INTEL_GUC_ACTION_DEREGISTER_COMMAND_TRANSPORT_BUFFER
,
100 /* Can't use generic send(), CT deregistration must go over MMIO */
101 err
= intel_guc_send_mmio(guc
, action
, ARRAY_SIZE(action
));
103 DRM_ERROR("CT: deregister %s buffer failed; owner=%d err=%d\n",
104 guc_ct_buffer_type_to_str(type
), owner
, err
);
108 static bool ctch_is_open(struct intel_guc_ct_channel
*ctch
)
110 return ctch
->vma
!= NULL
;
113 static int ctch_init(struct intel_guc
*guc
,
114 struct intel_guc_ct_channel
*ctch
)
116 struct i915_vma
*vma
;
121 GEM_BUG_ON(ctch
->vma
);
123 /* We allocate 1 page to hold both descriptors and both buffers.
124 * ___________.....................
126 * |___________| PAGE/4
127 * :___________....................:
129 * |___________| PAGE/4
130 * :_______________________________:
133 * |_______________________________|
136 * |_______________________________|
138 * Each message can use a maximum of 32 dwords and we don't expect to
139 * have more than 1 in flight at any time, so we have enough space.
140 * Some logic further ahead will rely on the fact that there is only 1
141 * page and that it is always mapped, so if the size is changed the
142 * other code will need updating as well.
146 vma
= intel_guc_allocate_vma(guc
, PAGE_SIZE
);
154 blob
= i915_gem_object_pin_map(vma
->obj
, I915_MAP_WB
);
159 DRM_DEBUG_DRIVER("CT: vma base=%#x\n", guc_ggtt_offset(ctch
->vma
));
161 /* store pointers to desc and cmds */
162 for (i
= 0; i
< ARRAY_SIZE(ctch
->ctbs
); i
++) {
163 GEM_BUG_ON((i
!= CTB_SEND
) && (i
!= CTB_RECV
));
164 ctch
->ctbs
[i
].desc
= blob
+ PAGE_SIZE
/4 * i
;
165 ctch
->ctbs
[i
].cmds
= blob
+ PAGE_SIZE
/4 * i
+ PAGE_SIZE
/2;
171 i915_vma_unpin_and_release(&ctch
->vma
);
173 DRM_DEBUG_DRIVER("CT: channel %d initialization failed; err=%d\n",
178 static void ctch_fini(struct intel_guc
*guc
,
179 struct intel_guc_ct_channel
*ctch
)
181 GEM_BUG_ON(!ctch
->vma
);
183 i915_gem_object_unpin_map(ctch
->vma
->obj
);
184 i915_vma_unpin_and_release(&ctch
->vma
);
187 static int ctch_open(struct intel_guc
*guc
,
188 struct intel_guc_ct_channel
*ctch
)
194 DRM_DEBUG_DRIVER("CT: channel %d reopen=%s\n",
195 ctch
->owner
, yesno(ctch_is_open(ctch
)));
198 err
= ctch_init(guc
, ctch
);
201 GEM_BUG_ON(!ctch
->vma
);
204 /* vma should be already allocated and map'ed */
205 base
= guc_ggtt_offset(ctch
->vma
);
207 /* (re)initialize descriptors
208 * cmds buffers are in the second half of the blob page
210 for (i
= 0; i
< ARRAY_SIZE(ctch
->ctbs
); i
++) {
211 GEM_BUG_ON((i
!= CTB_SEND
) && (i
!= CTB_RECV
));
212 guc_ct_buffer_desc_init(ctch
->ctbs
[i
].desc
,
213 base
+ PAGE_SIZE
/4 * i
+ PAGE_SIZE
/2,
218 /* register buffers, starting wirh RECV buffer
219 * descriptors are in first half of the blob
221 err
= guc_action_register_ct_buffer(guc
,
222 base
+ PAGE_SIZE
/4 * CTB_RECV
,
223 INTEL_GUC_CT_BUFFER_TYPE_RECV
);
227 err
= guc_action_register_ct_buffer(guc
,
228 base
+ PAGE_SIZE
/4 * CTB_SEND
,
229 INTEL_GUC_CT_BUFFER_TYPE_SEND
);
236 guc_action_deregister_ct_buffer(guc
,
238 INTEL_GUC_CT_BUFFER_TYPE_RECV
);
240 ctch_fini(guc
, ctch
);
242 DRM_ERROR("CT: can't open channel %d; err=%d\n", ctch
->owner
, err
);
246 static void ctch_close(struct intel_guc
*guc
,
247 struct intel_guc_ct_channel
*ctch
)
249 GEM_BUG_ON(!ctch_is_open(ctch
));
251 guc_action_deregister_ct_buffer(guc
,
253 INTEL_GUC_CT_BUFFER_TYPE_SEND
);
254 guc_action_deregister_ct_buffer(guc
,
256 INTEL_GUC_CT_BUFFER_TYPE_RECV
);
257 ctch_fini(guc
, ctch
);
260 static u32
ctch_get_next_fence(struct intel_guc_ct_channel
*ctch
)
262 /* For now it's trivial */
263 return ++ctch
->next_fence
;
266 static int ctb_write(struct intel_guc_ct_buffer
*ctb
,
268 u32 len
/* in dwords */,
271 struct guc_ct_buffer_desc
*desc
= ctb
->desc
;
272 u32 head
= desc
->head
/ 4; /* in dwords */
273 u32 tail
= desc
->tail
/ 4; /* in dwords */
274 u32 size
= desc
->size
/ 4; /* in dwords */
275 u32 used
; /* in dwords */
277 u32
*cmds
= ctb
->cmds
;
280 GEM_BUG_ON(desc
->size
% 4);
281 GEM_BUG_ON(desc
->head
% 4);
282 GEM_BUG_ON(desc
->tail
% 4);
283 GEM_BUG_ON(tail
>= size
);
286 * tail == head condition indicates empty. GuC FW does not support
287 * using up the entire buffer to get tail == head meaning full.
290 used
= (size
- head
) + tail
;
294 /* make sure there is a space including extra dw for the fence */
295 if (unlikely(used
+ len
+ 1 >= size
))
298 /* Write the message. The format is the following:
299 * DW0: header (including action code)
303 header
= (len
<< GUC_CT_MSG_LEN_SHIFT
) |
304 (GUC_CT_MSG_WRITE_FENCE_TO_DESC
) |
305 (action
[0] << GUC_CT_MSG_ACTION_SHIFT
);
308 tail
= (tail
+ 1) % size
;
311 tail
= (tail
+ 1) % size
;
313 for (i
= 1; i
< len
; i
++) {
314 cmds
[tail
] = action
[i
];
315 tail
= (tail
+ 1) % size
;
318 /* now update desc tail (back in bytes) */
319 desc
->tail
= tail
* 4;
320 GEM_BUG_ON(desc
->tail
> desc
->size
);
325 /* Wait for the response from the GuC.
326 * @fence: response fence
327 * @status: placeholder for status
328 * return: 0 response received (status is valid)
329 * -ETIMEDOUT no response within hardcoded timeout
330 * -EPROTO no response, ct buffer was in error
332 static int wait_for_response(struct guc_ct_buffer_desc
*desc
,
339 * Fast commands should complete in less than 10us, so sample quickly
340 * up to that length of time, then switch to a slower sleep-wait loop.
341 * No GuC command should ever take longer than 10ms.
343 #define done (READ_ONCE(desc->fence) == fence)
344 err
= wait_for_us(done
, 10);
346 err
= wait_for(done
, 10);
350 DRM_ERROR("CT: fence %u failed; reported fence=%u\n",
353 if (WARN_ON(desc
->is_in_error
)) {
354 /* Something went wrong with the messaging, try to reset
355 * the buffer and hope for the best
357 guc_ct_buffer_desc_reset(desc
);
362 *status
= desc
->status
;
366 static int ctch_send(struct intel_guc
*guc
,
367 struct intel_guc_ct_channel
*ctch
,
372 struct intel_guc_ct_buffer
*ctb
= &ctch
->ctbs
[CTB_SEND
];
373 struct guc_ct_buffer_desc
*desc
= ctb
->desc
;
377 GEM_BUG_ON(!ctch_is_open(ctch
));
379 GEM_BUG_ON(len
& ~GUC_CT_MSG_LEN_MASK
);
381 fence
= ctch_get_next_fence(ctch
);
382 err
= ctb_write(ctb
, action
, len
, fence
);
386 intel_guc_notify(guc
);
388 err
= wait_for_response(desc
, fence
, status
);
391 if (*status
!= INTEL_GUC_STATUS_SUCCESS
)
397 * Command Transport (CT) buffer based GuC send function.
399 static int intel_guc_send_ct(struct intel_guc
*guc
, const u32
*action
, u32 len
)
401 struct intel_guc_ct_channel
*ctch
= &guc
->ct
.host_channel
;
402 u32 status
= ~0; /* undefined */
405 mutex_lock(&guc
->send_mutex
);
407 err
= ctch_send(guc
, ctch
, action
, len
, &status
);
409 DRM_ERROR("CT: send action %#X failed; err=%d status=%#X\n",
410 action
[0], err
, status
);
413 mutex_unlock(&guc
->send_mutex
);
418 * Enable buffer based command transport
419 * Shall only be called for platforms with HAS_GUC_CT.
421 * return: 0 on success
422 * non-zero on failure
424 int intel_guc_enable_ct(struct intel_guc
*guc
)
426 struct drm_i915_private
*dev_priv
= guc_to_i915(guc
);
427 struct intel_guc_ct_channel
*ctch
= &guc
->ct
.host_channel
;
430 GEM_BUG_ON(!HAS_GUC_CT(dev_priv
));
432 err
= ctch_open(guc
, ctch
);
436 /* Switch into cmd transport buffer based send() */
437 guc
->send
= intel_guc_send_ct
;
438 DRM_INFO("CT: %s\n", enableddisabled(true));
443 * Disable buffer based command transport.
444 * Shall only be called for platforms with HAS_GUC_CT.
447 void intel_guc_disable_ct(struct intel_guc
*guc
)
449 struct drm_i915_private
*dev_priv
= guc_to_i915(guc
);
450 struct intel_guc_ct_channel
*ctch
= &guc
->ct
.host_channel
;
452 GEM_BUG_ON(!HAS_GUC_CT(dev_priv
));
454 if (!ctch_is_open(ctch
))
457 ctch_close(guc
, ctch
);
460 guc
->send
= intel_guc_send_nop
;
461 DRM_INFO("CT: %s\n", enableddisabled(false));