1 /*
2  Copyright (C) Intel Corp.  2006.  All Rights Reserved.
3  Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4  develop this 3D driver.
5 
6  Permission is hereby granted, free of charge, to any person obtaining
7  a copy of this software and associated documentation files (the
8  "Software"), to deal in the Software without restriction, including
9  without limitation the rights to use, copy, modify, merge, publish,
10  distribute, sublicense, and/or sell copies of the Software, and to
11  permit persons to whom the Software is furnished to do so, subject to
12  the following conditions:
13 
14  The above copyright notice and this permission notice (including the
15  next paragraph) shall be included in all copies or substantial
16  portions of the Software.
17 
18  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21  IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 
26  **********************************************************************/
27  /*
28   * Authors:
29   *   Keith Whitwell <keith@tungstengraphics.com>
30   */
31 
32 #include "brw_state.h"
33 #include "intel_batchbuffer.h"
34 #include "main/imports.h"
35 #include "glsl/ralloc.h"
36 
37 static void
brw_track_state_batch(struct brw_context * brw,enum state_struct_type type,uint32_t offset,int size)38 brw_track_state_batch(struct brw_context *brw,
39 		      enum state_struct_type type,
40 		      uint32_t offset,
41 		      int size)
42 {
43    struct intel_batchbuffer *batch = &brw->intel.batch;
44 
45    if (!brw->state_batch_list) {
46       /* Our structs are always aligned to at least 32 bytes, so
47        * our array doesn't need to be any larger
48        */
49       brw->state_batch_list = ralloc_size(brw, sizeof(*brw->state_batch_list) *
50 					  batch->bo->size / 32);
51    }
52 
53    brw->state_batch_list[brw->state_batch_count].offset = offset;
54    brw->state_batch_list[brw->state_batch_count].size = size;
55    brw->state_batch_list[brw->state_batch_count].type = type;
56    brw->state_batch_count++;
57 }
58 
59 /**
60  * Convenience function to populate a single drm_intel_aub_annotation data
61  * structure.
62  */
63 static inline void
make_annotation(drm_intel_aub_annotation * annotation,uint32_t type,uint32_t subtype,uint32_t ending_offset)64 make_annotation(drm_intel_aub_annotation *annotation, uint32_t type,
65                 uint32_t subtype, uint32_t ending_offset)
66 {
67    annotation->type = type;
68    annotation->subtype = subtype;
69    annotation->ending_offset = ending_offset;
70 }
71 
72 /**
73  * Generate a set of aub file annotations for the current batch buffer, and
74  * deliver them to DRM.
75  *
76  * The "used" section of the batch buffer (the portion containing batch
77  * commands) is annotated with AUB_TRACE_TYPE_BATCH.  The remainder of the
78  * batch buffer (which contains data structures pointed to by batch commands)
79  * is annotated according to the type of each data structure.
80  */
81 void
brw_annotate_aub(struct intel_context * intel)82 brw_annotate_aub(struct intel_context *intel)
83 {
84    struct brw_context *brw = brw_context(&intel->ctx);
85 
86    unsigned annotation_count = 2 * brw->state_batch_count + 1;
87    drm_intel_aub_annotation annotations[annotation_count];
88    int a = 0;
89    make_annotation(&annotations[a++], AUB_TRACE_TYPE_BATCH, 0,
90                    4*intel->batch.used);
91    for (int i = brw->state_batch_count; i-- > 0; ) {
92       uint32_t type = brw->state_batch_list[i].type;
93       uint32_t start_offset = brw->state_batch_list[i].offset;
94       uint32_t end_offset = start_offset + brw->state_batch_list[i].size;
95       make_annotation(&annotations[a++], AUB_TRACE_TYPE_NOTYPE, 0,
96                       start_offset);
97       make_annotation(&annotations[a++], AUB_TRACE_TYPE(type),
98                       AUB_TRACE_SUBTYPE(type), end_offset);
99    }
100    assert(a == annotation_count);
101    drm_intel_bufmgr_gem_set_aub_annotations(intel->batch.bo, annotations,
102                                             annotation_count);
103 }
104 
105 /**
106  * Allocates a block of space in the batchbuffer for indirect state.
107  *
108  * We don't want to allocate separate BOs for every bit of indirect
109  * state in the driver.  It means overallocating by a significant
110  * margin (4096 bytes, even if the object is just a 20-byte surface
111  * state), and more buffers to walk and count for aperture size checking.
112  *
113  * However, due to the restrictions inposed by the aperture size
114  * checking performance hacks, we can't have the batch point at a
115  * separate indirect state buffer, because once the batch points at
116  * it, no more relocations can be added to it.  So, we sneak these
117  * buffers in at the top of the batchbuffer.
118  */
119 void *
brw_state_batch(struct brw_context * brw,enum state_struct_type type,int size,int alignment,uint32_t * out_offset)120 brw_state_batch(struct brw_context *brw,
121 		enum state_struct_type type,
122 		int size,
123 		int alignment,
124 		uint32_t *out_offset)
125 {
126    struct intel_batchbuffer *batch = &brw->intel.batch;
127    uint32_t offset;
128 
129    assert(size < batch->bo->size);
130    offset = ROUND_DOWN_TO(batch->state_batch_offset - size, alignment);
131 
132    /* If allocating from the top would wrap below the batchbuffer, or
133     * if the batch's used space (plus the reserved pad) collides with our
134     * space, then flush and try again.
135     */
136    if (batch->state_batch_offset < size ||
137        offset < 4*batch->used + batch->reserved_space) {
138       intel_batchbuffer_flush(&brw->intel);
139       offset = ROUND_DOWN_TO(batch->state_batch_offset - size, alignment);
140    }
141 
142    batch->state_batch_offset = offset;
143 
144    if (unlikely(INTEL_DEBUG & (DEBUG_BATCH | DEBUG_AUB)))
145       brw_track_state_batch(brw, type, offset, size);
146 
147    *out_offset = offset;
148    return batch->map + (offset>>2);
149 }
150